そういうものなのか
[octcat@host: ~]$ node --version
v11.6.0
[octcat@host: ~]$ node
> 07
7
> 08
8
> 09
9
> 010
8
> 020
16
> 030
24
> 040
32
> 050
40
> 060
48
> 070
56
> 080
80
> 090
90
> 0100
64
追記
> [01234567, 012345678, 012345679].map(e => e);
[ 342391, 12345678, 12345679 ]
> [076, 077, 078, 079, 080].map(e => e);
[ 62, 63, 78, 79, 80 ]
> ['076', '077', '078', '079', '080'].map(e => e);
[ '076', '077', '078', '079', '080' ]
octcatz.js
// 'use strict';
const assert = require('assert');
let ds = x => console.log(`=> toString: ${x.toString()}`);
let di = x => console.log(`=> parseInt: ${parseInt(x)}`);
// なんとかコードをなんとかする
function f(code) {
console.log(`arg: ${code} (${typeof code})` );
ds(code);
di(code);
console.log();
return code;
}
// まぁこうだよねぇ
f('078');
f('077');
// なんだかんだで結果的にこんな感じになっちゃうらしい (まぁ可能性としてはわからなくも無い)
f(078);
f(077);
// https://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message
// https://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message
assert.equal(f('090'), '090'); // true
assert.equal(f(090), '090'); // true // ちょ
// assert.deepStrictEqual(f(090), '090'); // false
assert.equal(f('080'), '080'); // true
assert.equal(f(080), '080'); // true // ちょ
// assert.deepStrictEqual(f(080), '080'); // false
assert.equal(f('070'), '070'); // true
assert.equal(f(070), '070'); // false // あーあ
// assert.deepStrictEqual(f(070), '070'); // false
// ついで
// assert.equal(f('070'), 70); // true // んー
// assert.equal(f(70), '070'); // true // んー
[octcat@host: ~]$ node octcatz.js
arg: 078 (string)
=> toString: 078
=> parseInt: 78
arg: 077 (string)
=> toString: 077
=> parseInt: 77
arg: 78 (number)
=> toString: 78
=> parseInt: 78
arg: 63 (number)
=> toString: 63
=> parseInt: 63
arg: 090 (string)
=> toString: 090
=> parseInt: 90
arg: 90 (number)
=> toString: 90
=> parseInt: 90
arg: 080 (string)
=> toString: 080
=> parseInt: 80
arg: 80 (number)
=> toString: 80
=> parseInt: 80
arg: 070 (string)
=> toString: 070
=> parseInt: 70
arg: 56 (number)
=> toString: 56
=> parseInt: 56
assert.js:86
throw new AssertionError(obj);
^
AssertionError [ERR_ASSERTION]: 56 == '070'
at Object.<anonymous> (/Users/octcat/octcatz.js:37:8)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
at executeUserCode (internal/bootstrap/node.js:342:17)
at startExecution (internal/bootstrap/node.js:276:5)
at startup (internal/bootstrap/node.js:227:5)
なにがしたかったのか
なんとかコードみたいな値を扱う場合は十分注意しましょう...
ということで
- 基礎はしっかり学ぼう
- テストしよう(書こう)
- なんとなく動くプログラム書いて良いのは小学生までだよ
参考
MDNのparseInt
に各仕様についてのリンクが掲載されているので助かる
8進数の定義自体は初版から変わっていない (そりゃそうだ)
さらに追記
> parseInt('077')
77
> parseInt('077', 10)
77
> parseInt('077', 8) // はい
63
> parseInt(077)
63
> parseInt(077, 10)
63
> parseInt(077, 8) // うむ
51