回答編集履歴
5
ちょっと改良。
answer
CHANGED
@@ -54,4 +54,21 @@
|
|
54
54
|
|
55
55
|
console.log( checkNum3( 2346 ) ); // false
|
56
56
|
console.log( checkNum3( 2378 ) ); // true
|
57
|
+
```
|
58
|
+
|
59
|
+
ちょっと改良。(ちなみに「同じ数字が出ない」仕様でなくなった場合は Math.abs で処理している部分の符号をチェックする必要がある)
|
60
|
+
|
61
|
+
```js
|
62
|
+
function checkNum4( num ) {
|
63
|
+
const arr = num.toString().split( '' );
|
64
|
+
const l = arr.length - 2;
|
65
|
+
if ( l < 1 ) return true;
|
66
|
+
for ( let i = 0; i < l; i++ ) {
|
67
|
+
if ( ( Math.abs( arr[ i ] - arr[ i + 1 ] ) === 1 ) && ( Math.abs( arr[ i + 1 ] - arr[ i + 2 ] ) === 1 ) ) return false;
|
68
|
+
}
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
|
72
|
+
console.log( checkNum4( 2346 ) ); // false
|
73
|
+
console.log( checkNum4( 2378 ) ); // true
|
57
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/
|
74
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/4/](https://jsfiddle.net/q7eyp0uh/4/)
|
4
情報の追加。
answer
CHANGED
@@ -30,4 +30,28 @@
|
|
30
30
|
|
31
31
|
console.log( checkNum2( 2346 ) ); // false
|
32
32
|
console.log( checkNum2( 2378 ) ); // true
|
33
|
+
```
|
34
|
+
|
35
|
+
reduce 使わないほうが良いかな。
|
36
|
+
```js
|
37
|
+
function checkNum3( num ) {
|
38
|
+
const arr = num.toString().split( '' );
|
39
|
+
const l = arr.length
|
40
|
+
if ( l < 3 ) return true;
|
41
|
+
let prev = arr[ 0 ];
|
42
|
+
let tmp = 0;
|
43
|
+
for ( let i = 1; i < l; i++ ) {
|
44
|
+
if ( Math.abs( prev - arr[ i ] ) === 1 ) {
|
45
|
+
tmp++;
|
46
|
+
} else {
|
47
|
+
if ( tmp >= 2 ) return false;
|
48
|
+
tmp = 0;
|
49
|
+
}
|
50
|
+
prev = arr[ i ];
|
51
|
+
}
|
52
|
+
return true;
|
53
|
+
}
|
54
|
+
|
55
|
+
console.log( checkNum3( 2346 ) ); // false
|
56
|
+
console.log( checkNum3( 2378 ) ); // true
|
33
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/
|
57
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/3/](https://jsfiddle.net/q7eyp0uh/3/)
|
3
読みやすく修正。
answer
CHANGED
@@ -8,8 +8,10 @@
|
|
8
8
|
console.log( checkNum( 2346 ) ); // false
|
9
9
|
console.log( checkNum( 2378 ) ); // true
|
10
10
|
```
|
11
|
+
|
12
|
+
[Array.prototype.reduce()](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) でも書いてみた。
|
11
13
|
```js
|
12
|
-
function checkNum2( num ) {
|
14
|
+
function checkNum2( num ) {
|
13
15
|
const arr = num.toString().split( '' );
|
14
16
|
let tmp = 0;
|
15
17
|
let count = 0;
|
2
読みやすく修正。
answer
CHANGED
@@ -7,9 +7,8 @@
|
|
7
7
|
|
8
8
|
console.log( checkNum( 2346 ) ); // false
|
9
9
|
console.log( checkNum( 2378 ) ); // true
|
10
|
-
|
10
|
+
```
|
11
|
-
|
11
|
+
```js
|
12
|
-
|
13
12
|
function checkNum2( num ) { /* 追加 */
|
14
13
|
const arr = num.toString().split( '' );
|
15
14
|
let tmp = 0;
|
1
情報の追加。
answer
CHANGED
@@ -7,4 +7,26 @@
|
|
7
7
|
|
8
8
|
console.log( checkNum( 2346 ) ); // false
|
9
9
|
console.log( checkNum( 2378 ) ); // true
|
10
|
+
|
11
|
+
////
|
12
|
+
|
13
|
+
function checkNum2( num ) { /* 追加 */
|
14
|
+
const arr = num.toString().split( '' );
|
15
|
+
let tmp = 0;
|
16
|
+
let count = 0;
|
17
|
+
arr.reduce( ( pre, curr )=> {
|
18
|
+
if ( Math.abs( pre - curr ) === 1 ) {
|
19
|
+
tmp++;
|
20
|
+
} else {
|
21
|
+
if ( tmp >= 2 ) count++;
|
22
|
+
tmp = 0;
|
23
|
+
}
|
24
|
+
return curr;
|
25
|
+
} );
|
26
|
+
if ( tmp >= 2 ) count++;
|
27
|
+
return count === 0;
|
28
|
+
}
|
29
|
+
|
30
|
+
console.log( checkNum2( 2346 ) ); // false
|
31
|
+
console.log( checkNum2( 2378 ) ); // true
|
10
|
-
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/](https://jsfiddle.net/q7eyp0uh/)
|
32
|
+
```**動くサンプル:**[https://jsfiddle.net/q7eyp0uh/1/](https://jsfiddle.net/q7eyp0uh/1/)
|