2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244d7
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cNVR7Am'
|
|
|
|
forumTopicId: 16788
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 小于或等于运算符
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
使用<code>小于等于</code>运算符(<code><=</code>)比较两个数字的大小。如果在小于等于运算符左边的数字小于或者等于右边的数字,它会返回<code>true</code>。如果在小于等于运算符左边的数字大于右边的数字,它会返回<code>false</code>。与相等运算符类似,<code>小于等于</code>运算符会转换数据类型。
|
|
|
|
<strong>例如</strong>
|
|
|
|
|
|
|
|
```js
|
|
|
|
4 <= 5 // true
|
|
|
|
'7' <= 7 // true
|
|
|
|
5 <= 5 // true
|
|
|
|
3 <= 2 // false
|
|
|
|
'8' <= 4 // false
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
添加<code>小于等于</code>运算符到指定行,使得函数的返回语句有意义。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(0)</code>应该返回 "Smaller Than or Equal to 12"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(0) === "Smaller Than or Equal to 12");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(11)</code>应该返回 "Smaller Than or Equal to 12"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(11) === "Smaller Than or Equal to 12");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(12)</code>应该返回 "Smaller Than or Equal to 12"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(12) === "Smaller Than or Equal to 12");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(23)</code>应该返回 "Smaller Than or Equal to 24"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(23) === "Smaller Than or Equal to 24");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(24)</code>应该返回 "Smaller Than or Equal to 24"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(24) === "Smaller Than or Equal to 24");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(25)</code>应该返回 "More Than 24"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(25) === "More Than 24");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>testLessOrEqual(55)</code>应该返回 "More Than 24"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(testLessOrEqual(55) === "More Than 24");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: 你应该使用<code><=</code>运算符至少两。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(code.match(/val\s*<=\s*('|")*\d+('|")*/g).length > 1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function testLessOrEqual(val) {
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "Smaller Than or Equal to 12";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "Smaller Than or Equal to 24";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "More Than 24";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
testLessOrEqual(10);
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
function testLessOrEqual(val) {
|
|
|
|
if (val <= 12) { // Change this line
|
|
|
|
return "Smaller Than or Equal to 12";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val <= 24) { // Change this line
|
|
|
|
return "Smaller Than or Equal to 24";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "More Than 24";
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|