2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244d4
|
2021-03-13 17:31:57 +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/cp6GbH4'
|
|
|
|
forumTopicId: 16786
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: comparison-with-the-greater-than-operator
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
使用大于运算符(`>`)来比较两个数字。 如果大于运算符左边的数字大于右边的数字,将会返回 `true`。 否则,它返回 `false`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
与相等运算符一样,大于运算符在比较的时候,会转换值的数据类型。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
**例如:**
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
```js
|
2021-03-13 17:31:57 +00:00
|
|
|
5 > 3
|
|
|
|
7 > '3'
|
|
|
|
2 > 3
|
|
|
|
'1' > 9
|
2020-04-29 10:29:13 +00:00
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +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-13 17:31:57 +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-13 17:31:57 +00:00
|
|
|
`testGreaterThan(0)` 应该返回字符串 `10 or Under`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(0) === '10 or Under');
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(10)` 应该返回字符串 `10 or Under`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(testGreaterThan(10) === '10 or Under');
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(11)` 应该返回字符串 `Over 10`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(11) === 'Over 10');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(99)` 应该返回字符串 `Over 10`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(99) === 'Over 10');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(100)` 应该返回字符串 `Over 10`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(100) === 'Over 10');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(101)` 应该返回字符串 `Over 100`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(101) === 'Over 100');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
`testGreaterThan(150)` 应该返回字符串 `Over 100`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(testGreaterThan(150) === 'Over 100');
|
|
|
|
```
|
|
|
|
|
2021-03-13 17:31:57 +00:00
|
|
|
应该使用 `>` 运算符至少两次。
|
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(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 testGreaterThan(val) {
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "Over 100";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val) { // Change this line
|
|
|
|
return "Over 10";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "10 or Under";
|
|
|
|
}
|
|
|
|
|
|
|
|
testGreaterThan(10);
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
|
|
|
function testGreaterThan(val) {
|
|
|
|
if (val > 100) { // Change this line
|
|
|
|
return "Over 100";
|
|
|
|
}
|
|
|
|
if (val > 10) { // Change this line
|
|
|
|
return "Over 10";
|
|
|
|
}
|
|
|
|
return "10 or Under";
|
|
|
|
}
|
|
|
|
```
|