我正在研究一個使用for
循環的Codewars挑戰(這裏:https://www.codewars.com/kata/ten-green-bottles)。我仍然在編輯代碼,但是,無論我改變了多少,它仍然會說charAt
有錯誤。我的代碼是在這裏:十個綠色瓶子 - 字符錯誤
function tenGreenBottles(n) {
var numbers = [
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
];
var lyrics = "";
for (i = n - 1; i > -1; i--) {
var numberLine = numbers[i] + " green bottles hanging on the wall,\n";
var nextNumber = numbers[i - 1].charAt(0).toLowerCase() + numbers[i - 1].slice(1, numbers[i - 1].length);
if (i < 9) {
lyrics = lyrics + numberLine + numberLine + "And if one green bottle should accidentally fall,\n" + "There'll be " + nextNumber + " green bottles hanging on the wall.\n";
}
else {
lyrics = lyrics + "One green bottle hanging on the wall,\n" + "One green bottle hanging on the wall,\n" + "If that one green bottle should accidentally fall,\n" + "There'll be no green bottles hanging on the wall.";
}
}
return lyrics;
}