||
两边任何一个为true
,那么它就返回true
;否则返回false
。
逻辑或运算符由两个管道符号(|)组成。这个按键位于退格键和回车键之间。
下面这样的语句你应该很熟悉:
```js
if (num > 10) {
return "No";
}
if (num < 5) {
return "No";
}
return "Yes";
```
只有当num
大于等于 5 或小于等于 10 时,函数返回"Yes"。相同的逻辑可以简写成:
```js
if (num > 10 || num < 5) {
return "No";
}
return "Yes";
```
val
不在 10 和 20 之间(包括 10 和 20),返回"Outside"
。反之,返回"Inside"
。
||
操作符。
testString: assert(code.match(/\|\|/g).length === 1);
- text: 你应该只有一个if
表达式。
testString: assert(code.match(/if/g).length === 1);
- text: testLogicalOr(0)
应该返回 "Outside"。
testString: assert(testLogicalOr(0) === "Outside");
- text: testLogicalOr(9)
应该返回 "Outside"。
testString: assert(testLogicalOr(9) === "Outside");
- text: testLogicalOr(10)
应该返回 "Inside"。
testString: assert(testLogicalOr(10) === "Inside");
- text: testLogicalOr(15)
应该返回 "Inside"。
testString: assert(testLogicalOr(15) === "Inside");
- text: testLogicalOr(19)
应该返回 "Inside"。
testString: assert(testLogicalOr(19) === "Inside");
- text: testLogicalOr(20)
应该返回 "Inside"。
testString: assert(testLogicalOr(20) === "Inside");
- text: testLogicalOr(21)
应该返回 "Outside"。
testString: assert(testLogicalOr(21) === "Outside");
- text: testLogicalOr(25)
应该返回 "Outside"。
testString: assert(testLogicalOr(25) === "Outside");
```