有人寫了這個(非常可怕的)函數來將數值從0-999轉換爲英文單詞。爲什麼這個Javascript數組中的單元格錯誤?
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];
}
在這裏有一些相當奇怪的錯誤,我似乎無法找出原因。一些但不是全部的元素被放置在錯誤的單元格中。
我試圖顯示列表的contentes,它表現出了相當時髦結果:0-7有預期值
> 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)
也就是說,元素。要素68,69和78-999也具有期望值。元素64-67和70-77是空的。元素8-63具有不正確的值。
這個世界正在發生什麼?爲什麼15個單元格爲空,56個單元格不正確,其餘部分是否正確?
'22'八進制十進制實際上'18'是,但它是十九元,因爲數組是零索引。 –
@Peter:哈,哦,是啊,當然;) –