freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../basic-javascript/introducing-else-statements/index.md

1.4 KiB
Raw Blame History

title localeTitle
Introducing Else statements 介绍Else语句

介绍Else语句

问题解释:

· if语句组合到单个if/else语句中。

提示1

当第一个if语句返回false ,执行/评估下一段代码(如return ifelse语句)。

现在尝试解决问题

提示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); 

· 在repl.it上运行代码

代码说明

功能首先评估if条件val > 5的计算结果为true 。如果没有,则执行下一个语句( else { return "5 or smaller";})

资源