javascript - 我如何让它记录每次迭代?
问题描述
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))
这仅在第一次迭代时记录 sol 。如何让它返回或记录每次迭代?
解决方案
这是实现。
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);
推荐阅读
- python - 为什么在 macOS 上用 mido 播放 midi 时听不到声音?
- android - 如何使用 Span 提升特定角色的位置?
- angular - maxlength 在 ionic 4 android 的 ion-textarea 中不起作用
- batch-file - 在詹金斯管道中使用配置文件
- python - Python Pandas:如何根据过滤器将数字序列分配为列?
- html - 在不使用跨度的情况下更改段落内文本的颜色?
- spring - 为什么我在 Spring Boot 中得到 org.springframework.context.NoSuchMessageException?
- sql - oracle更新查询中如何更新空间
- timer - 如何在 Arduino 中使用 millis() 来获取图中两点之间的时间?
- python - 如何从 tkinter 中的按钮打开 GUI?