freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/introducing-else-statements...

2.4 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244da Introducing Else Statements 1 介绍其他声明

Description

if语句的条件为真时,将执行其后面的代码块。当那个条件是假的时候怎么办?通常什么都不会发生。使用else语句,可以执行备用代码块。
ifnum> 10{
返回“大于10”;
} else {
返回“10或更少”;
}

Instructions

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

Tests

tests:
  - text: 您应该只在编辑器中有一个<code>if</code>语句
    testString: 'assert(code.match(/if/g).length === 1, "You should only have one <code>if</code> statement in the editor");'
  - text: 你应该使用<code>else</code>语句
    testString: 'assert(/else/g.test(code), "You should use an <code>else</code> statement");'
  - text: <code>testElse(4)</code>应返回“5或更小”
    testString: 'assert(testElse(4) === "5 or Smaller", "<code>testElse(4)</code> should return "5 or Smaller"");'
  - text: <code>testElse(5)</code>应返回“5或更小”
    testString: 'assert(testElse(5) === "5 or Smaller", "<code>testElse(5)</code> should return "5 or Smaller"");'
  - text: <code>testElse(6)</code>应该返回“大于5”
    testString: 'assert(testElse(6) === "Bigger than 5", "<code>testElse(6)</code> should return "Bigger than 5"");'
  - text: <code>testElse(10)</code>应该返回“大于5”
    testString: 'assert(testElse(10) === "Bigger than 5", "<code>testElse(10)</code> should return "Bigger than 5"");'
  - text: 请勿更改行上方或下方的代码。
    testString: 'assert(/var result = "";/.test(code) && /return result;/.test(code), "Do not change the code above or below the lines.");'

Challenge Seed

function testElse(val) {
  var result = "";
  // Only change code below this line

  if (val > 5) {
    result = "Bigger than 5";
  }

  if (val <= 5) {
    result = "5 or Smaller";
  }

  // Only change code above this line
  return result;
}

// Change this value to test
testElse(4);

Solution

// solution required