teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

typo削除

2016/04/28 12:24

投稿

think49
think49

スコア18194

answer CHANGED
@@ -1,6 +1,3 @@
1
- 10 > 1 === true ですね。
2
-
3
-
4
1
  ### 比較演算子の文字列比較
5
2
 
6
3
  文字列比較の場合は前方から1文字ずつUnicodeポイントを比較します。

1

compareStrings\('10', '1'\) === '>' になっていなかった不具合を修正

2016/04/28 12:23

投稿

think49
think49

スコア18194

answer CHANGED
@@ -1,6 +1,13 @@
1
+ 10 > 1 === true ですね。
2
+
3
+
4
+ ### 比較演算子の文字列比較
5
+
1
6
  文字列比較の場合は前方から1文字ずつUnicodeポイントを比較します。
2
7
  あくまでも**1文字ずつ比較する**ので `'10' > '2' === false` であり、人間が直感的に考える比較とは異なる点に注意が必要です。
3
8
 
9
+ ### 比較演算子の擬似コード
10
+
4
11
  ```JavaScript
5
12
  'use strict';
6
13
  function compareStrings (string1, string2) {
@@ -21,12 +28,20 @@
21
28
  return '<';
22
29
  }
23
30
  }
31
+
32
+ return string1.length > string2.length ? '>' : '<';
24
33
  }
25
34
 
26
35
  console.log('abc' < 'def'); // true
27
36
  console.log(compareStrings('abc', 'def')); // "<"
28
37
  console.log('10' > '2'); // false
29
38
  console.log(compareStrings('10', '2')); // "<"
39
+ console.log('10' > '1'); // true
40
+ console.log(compareStrings('10', '1')); // ">"
30
41
  ```
31
42
 
43
+ ### 更新履歴
44
+
45
+ - 2016/04/28 21:21 `compareStrings('10', '1') === '>'` になっていなかった不具合を修正
46
+
32
47
  Re: aaaaaaaa さん