私はfor
ループを利用するコードワードのチャレンジ(ここではhttps://www.codewars.com/kata/ten-green-bottles)に取り組んでいます。私はまだコードを編集していますが、どれほど変更しても、それはcharAt
のエラーが続いています。私のコードはここにある:グリーンボトル10個 - 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;
}