为什么这个 Javascript 数组中的项目位于错误的单元格中?

发布于 2024-12-14 16:08:13 字数 1382 浏览 0 评论 0原文

有人写了这个(非常糟糕)函数来将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅黛梨妆こ 2024-12-21 16:08:16

在 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.

地狱即天堂 2024-12-21 16:08:15

在非严格代码中以 0 开头的数字文字可能会导致该数字被解析为八进制文字(基数为 8)。例如,010 被解析为值为 8 的数字。

八进制文字现已弃用,并且不会在严格模式下解析为八进制,使用 "use strict"

function a() {
    alert(010);
    // -> 8
} 
function b() {
    "use strict";
    alert(010);
    // -> 10
} 

并非所有浏览器都支持严格模式,因此现在只需更改代码以确保数字不会开始与 0,或将它们包装在字符串中:

list[  21 ] = "something"
list["022"] = "something else";

字符串之所以有效,是因为八进制数 不被强制

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 value 8.

Octal literals are now deprecated and will not be parsed as octals in strict mode, using "use strict":

function a() {
    alert(010);
    // -> 8
} 
function b() {
    "use strict";
    alert(010);
    // -> 10
} 

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:

list[  21 ] = "something"
list["022"] = "something else";

Strings work because octal numbers are not coerced.

莫相离 2024-12-21 16:08:15

0 前缀在 JavaScript 中表示八进制,因此 022 是八进制:

022 = 2*8^1+2*8^0 = 18

并且由于第一个索引 i 0,18 给出了第 19 个元素。

The 0-prefix means octal in JavaScript, so 022 is octal:

022 = 2*8^1+2*8^0 = 18

And since the first index i 0, 18 gives the 19th element.

独闯女儿国 2024-12-21 16:08:14

0 开头的数字文字被解释为八进制值(如果可以的话)——即以 8 为基数的数字。这是 Javascript 中的情况,C, C++PHPPerl, Bash 和许多其他语言。

现在,基数 8 22 是基数 10 18,因此您无法访问您认为的元素。前八个数组元素很好,因为自然地,基数 8 0 也是基数 10 0,依此类推,直到 7。像 069 这样的值不会引起混乱,因为它不能表示以 8 为基数的任何内容,因此 Javascript 会回退到以 10 为基数的值。哎呀!


我建议使用空格来对齐:

function getNumberWords(number) {
  var list = new Array(1000);
  list[  0] = "zero";
  list[  1] = "one";
  list[  2] = "two";
  list[  3] = "three";
  ///skip a few
  list[ 99] = "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];
}

我还建议创建一个新函数来动态生成字符串;它不应该是费力的,当然也不会比在每次调用时创建这个数组更费力。

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-10 18 so you're not accessing the elements that you think you are. Your first eight array elements were fine because, naturally, base-8 0 is also base-10 0, and so on up to 7. A value like 069 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:

function getNumberWords(number) {
  var list = new Array(1000);
  list[  0] = "zero";
  list[  1] = "one";
  list[  2] = "two";
  list[  3] = "three";
  ///skip a few
  list[ 99] = "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];
}

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.

_蜘蛛 2024-12-21 16:08:14

在 Javascript 中,022 表示八进制的 22(十进制的 18)。

维基百科上的八进制数字系统

In Javascript 022 means 22 in octal (18 in decimal).

Octal numeral system on Wikipedia

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文