1.4 KiB
1.4 KiB
title | localeTitle |
---|---|
Introducing Else statements | 介绍Else语句 |
介绍Else语句
问题解释:
· 将if
语句组合到单个if/else
语句中。
提示1
当第一个if
语句返回false
,执行/评估下一段代码(如return
, if
或else
语句)。
现在尝试解决问题
提示2
有时if
( condition
)语句可以用else {code to execute instead}
语句else {code to execute instead}
(实质上你告诉你的函数做_“y”,如果它不能做“x”而不是多次指定“x”_ )。
现在尝试解决问题
扰流板警报!
提前解决!
基本代码解决方案
function testElse(val) {
var result = "";
// Only change code below this line
if (val > 5) {
result = "Bigger than 5";
}
else {
result = "5 or smaller";
}
// Only change code above this line
return result;
}
// Change this value to test
testElse(4);
代码说明
功能首先评估if
条件val > 5
的计算结果为true
。如果没有,则执行下一个语句( else { return "5 or smaller";})
。