2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244d5
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 大于或等于运算符
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/c6KBqtV'
|
|
|
|
forumTopicId: 16785
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: comparison-with-the-greater-than-or-equal-to-operator
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
使用大于等于运算符(`>=`)来比较两个数字的大小。 如果大于等于运算符左边的数字比右边的数字大或者相等,会返回 `true`。 否则,会返回 `false`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
与相等运算符相似,大于等于运算符在比较的时候会转换值的数据类型。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
**例如:**
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
```js
|
2021-03-15 03:20:39 +00:00
|
|
|
6 >= 6
|
|
|
|
7 >= '3'
|
|
|
|
2 >= 3
|
|
|
|
'7' >= 9
|
2020-04-29 10:29:13 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
按顺序,这些表达式会返回 `true`、`true`、`false` 和 `false`。
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
添加大于等于运算符到指定行,使得函数的返回语句有意义。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(0)` 应该返回字符串 `Less than 10`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(testGreaterOrEqual(0) === 'Less than 10');
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(9)` 应该返回字符串 `Less than 10`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterOrEqual(9) === 'Less than 10');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(10)` 应该返回字符串 `10 or Over`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterOrEqual(10) === '10 or Over');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(11)` 应该返回字符串 `10 or Over`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterOrEqual(11) === '10 or Over');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(19)` 应该返回字符串 `10 or Over`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterOrEqual(19) === '10 or Over');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(100)` 应该返回字符串 `20 or Over`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterOrEqual(100) === '20 or Over');
|
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`testGreaterOrEqual(21)` 应该返回字符串 `20 or Over`。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(testGreaterOrEqual(21) === '20 or Over');
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
应该使用 `>=` 运算符至少两次。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(code.match(/val\s*>=\s*('|")*\d+('|")*/g).length > 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function testGreaterOrEqual(val) {
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "20 or Over";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "10 or Over";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Less than 10";
|
|
|
|
}
|
|
|
|
|
|
|
|
testGreaterOrEqual(10);
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function testGreaterOrEqual(val) {
|
|
|
|
if (val >= 20) { // Change this line
|
|
|
|
return "20 or Over";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val >= 10) { // Change this line
|
|
|
|
return "10 or Over";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Less than 10";
|
|
|
|
}
|
|
|
|
```
|