WEB前端开发,不知道这串代码为什么运行不了

WEB前端开发,不知道这串代码为什么运行不了,希望知道的解释一下

img


function fn(year) {
    if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
        console.log(year+'是瑞年')
        return true
    }else{
        console.log(year+'是平年')
        return false
    }
}
fn(2022)

运行不了是什么意思,无法console?还是无法调用到函数?
从你给出的片段看,1、是未调用isLeapYear这个函数。2、console.log写在了return flag下,因此console是不会打印的。
我改写了一下您的函数,望参考:

function isLeapYear(year) {
  return year % 100 != 0 && year % 4 == 0 || year % 400 == 0;
}

isLeapYear(2022);

首先,你本身就没有调用函数,也没有传参,所以没有任何输出和报错;

img

当调用函数之后, 也还是没有报错和输出,是因为你console.log输出之前用了return (有停止运行后面代码的功能);

img

最后把return注释掉之后,报错,是因为后面的代码执行了 ,但是你的格式不对

img

if 和三目运算符都有判断的功能,这样写感觉有点多余了

img

把console.log写在函数外面