为什么这个 Javascript 数组中的项目位于错误的单元格中?
有人写了这个(非常糟糕)函数来将 0-999 之间的数值转换为英文单词。
function getNumberWords(number) {
var list = new Array(1000);
list[000] = "zero";
list[001] = "one";
list[002] = "two";
list[003] = "three";
///skip a few
list[099] = "ninety nine";
list[100] = "one hundred";
list[101] = "one hundred and one";
///skip a few more
list[997] = "nine hundred and ninety seven";
list[998] = "nine hundred and ninety eight";
list[999] = "nine hundred and ninety nine";
return list[number];
}
这里有一些相当奇怪的错误,我似乎无法找出原因。一些(但不是全部)元素被放置在错误的单元格中。
我尝试显示列表的内容,它显示了一个非常奇怪的结果:
> list.toString();
"zero,one,two,three,four,five,six,seven,ten,eleven,twelve,thirteen,fourteen,
fifteen,sixteen,seventeen,twenty,twenty one,twenty two,twenty three,twenty four,
twenty five,twenty six,twenty seven,thirty,thirty one,thirty two,thirty three,
thirty four,thirty five,thirty six,thirty seven,forty,forty one,forty two,"
///(skip a few)
"sixty six,sixty seven,seventy,seventy one,seventy two,seventy three,seventy four,
seventy five,seventy six,seventy seven,,,,,sixty eight,sixty nine,,,,,,,,,
seventy eight,seventy nine,eighty,eighty one,eighty two,eighty three,eighty four,"
///(and so on)
也就是说,元素 0-7 具有预期值。元素 68、69 和 78-999 也具有预期值。元素 64-67 和 70-77 为空。元素 8-63 的值不正确。
这里到底发生了什么?为什么 15 个单元格是空的,56 个单元格不正确,而其余的单元格是正确的?
Somebody wrote this (very terrible) function to translate a numeric value from 0-999 to English words.
function getNumberWords(number) {
var list = new Array(1000);
list[000] = "zero";
list[001] = "one";
list[002] = "two";
list[003] = "three";
///skip a few
list[099] = "ninety nine";
list[100] = "one hundred";
list[101] = "one hundred and one";
///skip a few more
list[997] = "nine hundred and ninety seven";
list[998] = "nine hundred and ninety eight";
list[999] = "nine hundred and ninety nine";
return list[number];
}
There is some rather odd bug in here that I can't seem to figure out the cause of. Some, but not all of the elements are placed in the wrong cell.
I tried displaying the contentes of the list and it showed a pretty funky result:
> list.toString();
"zero,one,two,three,four,five,six,seven,ten,eleven,twelve,thirteen,fourteen,
fifteen,sixteen,seventeen,twenty,twenty one,twenty two,twenty three,twenty four,
twenty five,twenty six,twenty seven,thirty,thirty one,thirty two,thirty three,
thirty four,thirty five,thirty six,thirty seven,forty,forty one,forty two,"
///(skip a few)
"sixty six,sixty seven,seventy,seventy one,seventy two,seventy three,seventy four,
seventy five,seventy six,seventy seven,,,,,sixty eight,sixty nine,,,,,,,,,
seventy eight,seventy nine,eighty,eighty one,eighty two,eighty three,eighty four,"
///(and so on)
That is, elements 0-7 have the expected value. Elements 68, 69, and 78-999 also have the expected values. Elements 64-67 and 70-77 are empty. Elements 8-63 have incorrect values.
What in the world is going on here? Why are 15 cells empty, 56 cells incorrect, and the rest correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 JavaScript 中,以
0
开头的数字被解释为八进制(基数为 8)。例如,
010 === 8
。奇怪的是,如果无法以八进制表示该数字,即使该数字具有八进制前缀,JavaScript 也会将该数字解释为十进制(以 10 为基数)。
例如,
08 === 8
。In JavaScript, numbers beginning with
0
are interpreted as octal (base 8).For example,
010 === 8
.Oddly, JavaScript will interpret the number as decimal (base 10) even if it has an octal prefix, if the number is impossible to reach in octal.
For example,
08 === 8
.在非严格代码中以
0
开头的数字文字可能会导致该数字被解析为八进制文字(基数为 8)。例如,010
被解析为值为8
的数字。八进制文字现已弃用,并且不会在严格模式下解析为八进制,使用
"use strict"
:并非所有浏览器都支持严格模式,因此现在只需更改代码以确保数字不会开始与 0,或将它们包装在字符串中:
字符串之所以有效,是因为八进制数 不被强制。
Starting a number literal with a
0
in non-strict code may cause that number to be parsed as an Octal literal (base 8). For instance,010
is parsed to a number with the value8
.Octal literals are now deprecated and will not be parsed as octals in strict mode, using
"use strict"
:Not all browser support strict mode, so for now just change your code to make sure numbers do not start with a 0, or wrap them in strings:
Strings work because octal numbers are not coerced.
0 前缀在 JavaScript 中表示八进制,因此
022
是八进制:并且由于第一个索引 i 0,18 给出了第 19 个元素。
The 0-prefix means octal in JavaScript, so
022
is octal:And since the first index i 0, 18 gives the 19th element.
以
0
开头的数字文字被解释为八进制值(如果可以的话)——即以 8 为基数的数字。这是 Javascript 中的情况,C, C++、PHP、Perl, Bash 和许多其他语言。现在,基数 8
22
是基数 1018
,因此您无法访问您认为的元素。前八个数组元素很好,因为自然地,基数 80
也是基数 100
,依此类推,直到7
。像069
这样的值不会引起混乱,因为它不能表示以 8 为基数的任何内容,因此 Javascript 会回退到以 10 为基数的值。哎呀!我建议使用空格来对齐:
我还建议创建一个新函数来动态生成字符串;它不应该是费力的,当然也不会比在每次调用时创建这个数组更费力。
Numeric literals starting with
0
are interpreted as octal values (if they can be) — that is, numbers in base-8. This is the case in Javascript, C, C++, PHP, Perl, Bash and many other languages.Now, base-8
22
is base-1018
so you're not accessing the elements that you think you are. Your first eight array elements were fine because, naturally, base-80
is also base-100
, and so on up to7
. A value like069
did not cause confusion because it cannot represent anything in base-8, so Javascript falls back to base-10. Yuck!I suggest using spacing for alignment instead:
I also suggest making a new function that generates the strings on-the-fly; it shouldn't be taxing and certainly no more so than creating this array on every call.
在 Javascript 中,
022
表示八进制的22
(十进制的18
)。维基百科上的八进制数字系统
In Javascript
022
means22
in octal (18
in decimal).Octal numeral system on Wikipedia