3.1 KiB
3.1 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
| id | title | challengeType | videoUrl | forumTopicId | localeTitle |
|---|---|---|---|---|---|
| cf1111c1c12feddfaeb3bdef | Use Conditional Logic with If Statements | 1 | https://scrimba.com/c/cy87mf3 | 18348 | 用 if 语句来表达条件逻辑 |
Description
If语句用于在代码中做条件判断。关键字if告诉 JavaScript 在小括号中的条件为真的情况下去执行定义在大括号里面的代码。这种条件被称为Boolean条件,因为他们只可能是true(真)或false(假)。
当条件的计算结果为true,程序执行大括号内的语句。当布尔条件的计算结果为false,大括号内的代码将不会执行。
伪代码
if(条件为真){示例
语句被执行
}
function test (myCondition) {
if (myCondition) {
return "It was true";
}
return "It was false";
}
test(true); // returns "It was true"
test(false); // returns "It was false"
当test被调用,并且传递进来的参数值为true,if语句会计算myCondition的结果,看它是真还是假。如果条件为true,函数会返回"It was true"。当test被调用,并且传递进来的参数值为false,myCondition不 为true,并且不执行大括号后面的语句,函数返回"It was false"。
Instructions
if语句,如果该参数wasThatTrue值为true,返回"Yes, that was true",否则,并返回"No, that was false"。
Tests
tests:
- text: <code>trueOrFalse</code>应该是一个函数。
testString: assert(typeof trueOrFalse === "function");
- text: <code>trueOrFalse(true)</code>应该返回一个字符串。
testString: assert(typeof trueOrFalse(true) === "string");
- text: <code>trueOrFalse(false)</code>应该返回一个字符串。
testString: assert(typeof trueOrFalse(false) === "string");
- text: <code>trueOrFalse(true)</code>应该返回 "Yes, that was true"。
testString: assert(trueOrFalse(true) === "Yes, that was true");
- text: <code>trueOrFalse(false)</code>应该返回 "No, that was false"。
testString: assert(trueOrFalse(false) === "No, that was false");
Challenge Seed
// Example
function ourTrueOrFalse(isItTrue) {
if (isItTrue) {
return "Yes, it's true";
}
return "No, it's false";
}
// Setup
function trueOrFalse(wasThatTrue) {
// Only change code below this line.
// Only change code above this line.
}
// Change this value to test
trueOrFalse(true);
Solution
function trueOrFalse(wasThatTrue) {
if (wasThatTrue) {
return "Yes, that was true";
}
return "No, that was false";
}