--- id: cf1111c1c12feddfaeb3bdef title: Use Conditional Logic with If Statements challengeType: 1 videoUrl: '' localeTitle: 使用条件逻辑和If语句 --- ## Description
If语句用于在代码中做出决定。关键字if告诉JavaScript在括号中定义的特定条件下执行花括号中的代码。这些条件称为Boolean条件,它们可能只是truefalse 。当条件计算结果为true ,程序将执行花括号内的语句。当布尔条件的计算结果为false ,大括号内的语句将不会执行。 伪代码
if( condition为true ){
声明被执行
}
功能测试(myCondition){
if(myCondition){
回归“这是真的”;
}
返回“这是假的”;
}
测试(真); //返回“这是真的”
测试(假); //返回“这是假的”
当使用值true调用testif语句将评估myCondition以查看它是否为true 。因为它是true ,函数返回"It was true" 。当我们使用false值调用test时, myCondition 不为 true并且不执行花括号中的语句,函数返回"It was false"
## Instructions
在函数内部创建一个if语句"Yes, that was true"如果参数wasThatTruetrue则返回"Yes, that was true" "No, that was false"否则返回"No, that was false"
## Tests
```yml tests: - text: trueOrFalse应该是一个函数 testString: 'assert(typeof trueOrFalse === "function", "trueOrFalse should be a function");' - text: trueOrFalse(true)应该返回一个字符串 testString: 'assert(typeof trueOrFalse(true) === "string", "trueOrFalse(true) should return a string");' - text: trueOrFalse(false)应该返回一个字符串 testString: 'assert(typeof trueOrFalse(false) === "string", "trueOrFalse(false) should return a string");' - text: trueOrFalse(true)应该返回“是的,那是真的” testString: 'assert(trueOrFalse(true) === "Yes, that was true", "trueOrFalse(true) should return "Yes, that was true"");' - text: trueOrFalse(false)应该返回“No,that was false” testString: 'assert(trueOrFalse(false) === "No, that was false", "trueOrFalse(false) should return "No, that was false"");' ```
## Challenge Seed
```js // 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
```js // solution required ```