回答編集履歴
5
ちょっと改良。
test
CHANGED
@@ -110,4 +110,38 @@
|
|
110
110
|
|
111
111
|
console.log( checkNum3( 2378 ) ); // true
|
112
112
|
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
ちょっと改良。(ちなみに「同じ数字が出ない」仕様でなくなった場合は Math.abs で処理している部分の符号をチェックする必要がある)
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
```js
|
122
|
+
|
123
|
+
function checkNum4( num ) {
|
124
|
+
|
125
|
+
const arr = num.toString().split( '' );
|
126
|
+
|
127
|
+
const l = arr.length - 2;
|
128
|
+
|
129
|
+
if ( l < 1 ) return true;
|
130
|
+
|
131
|
+
for ( let i = 0; i < l; i++ ) {
|
132
|
+
|
133
|
+
if ( ( Math.abs( arr[ i ] - arr[ i + 1 ] ) === 1 ) && ( Math.abs( arr[ i + 1 ] - arr[ i + 2 ] ) === 1 ) ) return false;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
return true;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
console.log( checkNum4( 2346 ) ); // false
|
144
|
+
|
145
|
+
console.log( checkNum4( 2378 ) ); // true
|
146
|
+
|
113
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/
|
147
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/4/](https://jsfiddle.net/q7eyp0uh/4/)
|
4
情報の追加。
test
CHANGED
@@ -62,4 +62,52 @@
|
|
62
62
|
|
63
63
|
console.log( checkNum2( 2378 ) ); // true
|
64
64
|
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
reduce 使わないほうが良いかな。
|
70
|
+
|
71
|
+
```js
|
72
|
+
|
73
|
+
function checkNum3( num ) {
|
74
|
+
|
75
|
+
const arr = num.toString().split( '' );
|
76
|
+
|
77
|
+
const l = arr.length
|
78
|
+
|
79
|
+
if ( l < 3 ) return true;
|
80
|
+
|
81
|
+
let prev = arr[ 0 ];
|
82
|
+
|
83
|
+
let tmp = 0;
|
84
|
+
|
85
|
+
for ( let i = 1; i < l; i++ ) {
|
86
|
+
|
87
|
+
if ( Math.abs( prev - arr[ i ] ) === 1 ) {
|
88
|
+
|
89
|
+
tmp++;
|
90
|
+
|
91
|
+
} else {
|
92
|
+
|
93
|
+
if ( tmp >= 2 ) return false;
|
94
|
+
|
95
|
+
tmp = 0;
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
prev = arr[ i ];
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
return true;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
console.log( checkNum3( 2346 ) ); // false
|
110
|
+
|
111
|
+
console.log( checkNum3( 2378 ) ); // true
|
112
|
+
|
65
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/
|
113
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/3/](https://jsfiddle.net/q7eyp0uh/3/)
|
3
読みやすく修正。
test
CHANGED
@@ -18,9 +18,13 @@
|
|
18
18
|
|
19
19
|
```
|
20
20
|
|
21
|
+
|
22
|
+
|
23
|
+
[Array.prototype.reduce()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) でも書いてみた。
|
24
|
+
|
21
25
|
```js
|
22
26
|
|
23
|
-
function checkNum2( num ) {
|
27
|
+
function checkNum2( num ) {
|
24
28
|
|
25
29
|
const arr = num.toString().split( '' );
|
26
30
|
|
2
読みやすく修正。
test
CHANGED
@@ -16,11 +16,9 @@
|
|
16
16
|
|
17
17
|
console.log( checkNum( 2378 ) ); // true
|
18
18
|
|
19
|
+
```
|
19
20
|
|
20
|
-
|
21
|
-
|
21
|
+
```js
|
22
|
-
|
23
|
-
|
24
22
|
|
25
23
|
function checkNum2( num ) { /* 追加 */
|
26
24
|
|
1
情報の追加。
test
CHANGED
@@ -16,4 +16,48 @@
|
|
16
16
|
|
17
17
|
console.log( checkNum( 2378 ) ); // true
|
18
18
|
|
19
|
+
|
20
|
+
|
21
|
+
////
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
function checkNum2( num ) { /* 追加 */
|
26
|
+
|
27
|
+
const arr = num.toString().split( '' );
|
28
|
+
|
29
|
+
let tmp = 0;
|
30
|
+
|
31
|
+
let count = 0;
|
32
|
+
|
33
|
+
arr.reduce( ( pre, curr )=> {
|
34
|
+
|
35
|
+
if ( Math.abs( pre - curr ) === 1 ) {
|
36
|
+
|
37
|
+
tmp++;
|
38
|
+
|
39
|
+
} else {
|
40
|
+
|
41
|
+
if ( tmp >= 2 ) count++;
|
42
|
+
|
43
|
+
tmp = 0;
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
return curr;
|
48
|
+
|
49
|
+
} );
|
50
|
+
|
51
|
+
if ( tmp >= 2 ) count++;
|
52
|
+
|
53
|
+
return count === 0;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
console.log( checkNum2( 2346 ) ); // false
|
60
|
+
|
61
|
+
console.log( checkNum2( 2378 ) ); // true
|
62
|
+
|
19
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/](https://jsfiddle.net/q7eyp0uh/)
|
63
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/1/](https://jsfiddle.net/q7eyp0uh/1/)
|