将三元运算符的变量转换为if-else语句

时间:2017-01-15 14:34:12

标签: javascript if-statement operator-keyword ternary

x = (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);  
alert(x);

大家好,如何将此变量x转换为if-else语句,以true或false结果返回变量?

非常感谢

1 个答案:

答案 0 :(得分:1)

year = 2010;

if(year % 100 === 0)
    x = (year % 400 === 0);
else
    x = (year % 4 === 0);  
alert(x);

if(year % 100 === 0)
    x = (year % 400 === 0);
else
    x = (year % 4 === 0);  
alert(x);