Javascript 如何让它记录每次迭代?
这只会在第一次迭代中记录sol。如何让它返回或记录每次迭代?以下是实现Javascript 如何让它记录每次迭代?,javascript,Javascript,这只会在第一次迭代中记录sol。如何让它返回或记录每次迭代?以下是实现 function annoyingSong(start){ for (let i = start; i >= 0; i--) { sol = `${i} bottles of soda on the wall, ${i} bottles of soda, take one down pass it around ${i-1} bottles of soda on the wall` return so
function annoyingSong(start){
for (let i = start; i >= 0; i--) {
sol = `${i} bottles of soda on the wall, ${i} bottles of soda, take one down pass it around ${i-1}
bottles of soda on the wall`
return sol;
}
}
console.log(annoyingSong(20))
提示:当你突然在代码的中间突然出现<代码>返回<代码>时会发生什么?提示:<代码>恼人的歌曲(开始,FN)< /代码>,然后<代码> fn('${i}瓶…)<代码> > OHHHHHHH,返回在我的循环中!代码>返回就像拉动降落伞上的绳索一样。在合适的时间之前不要这样做。如果你有任何问题,请给我发电子邮件。work@pm.me
function annoyingSong(start, fn) {
for (let i = start; i >= 0; i--) {
console.log(fn(i));
}
}
function fn(count) {
return `${count} bottles of soda on the wall, ${count} bottles of soda, take one down pass it around ${count - 1} bottles of soda on the wall`;
}
annoyingSong(20, fn);