本文介绍了用while循环通知数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用while循环,我想用数组中的名称来提醒名称.我还必须将每个名称都放在一个警报中.
Using while loop, I want to alert names with their number from an array. I also have to put every name in a single alert.
脚本:
var voetbalteam = 1;
var spelers = ["Nobert Alblas", "Kostas Lamprou", "André Onana", "Benjamin van Leer", "Léon Bergsma", "Damil Dankerlui", "Matthijs de Ligt", "Mitchell Dijks", "Luis Orejuela", "Daley Sinkgraven", "Joël Veltman", "Nick Viergever", "Max Wöber", "Deyovaisio Zeefuik", "Frenkie de Jong", "Siem de Jong", "Carel Eiting", "Noussair Mazaoui", "Lasse Schöne", "Donny van de Beek", "Klaas Jan Huntelaar", "Kasper Dolberg", "Justin Kluivert"];
while ((voetbalteam <= 23) + (spelers >= 0)){
alert("Ik ben " + spelers[spelers] + " En ik ben de " + voetbalteam + "e speler.");
spelers ++;
voetbalteam ++;
}
推荐答案
使用 forEach()遍历数组.
var voetbalteam = 1;
var spelers = ["Nobert Alblas", "Kostas Lamprou", "André Onana", "Benjamin van Leer", "Léon Bergsma", "Damil Dankerlui", "Matthijs de Ligt", "Mitchell Dijks", "Luis Orejuela", "Daley Sinkgraven", "Joël Veltman", "Nick Viergever", "Max Wöber", "Deyovaisio Zeefuik", "Frenkie de Jong", "Siem de Jong", "Carel Eiting", "Noussair Mazaoui", "Lasse Schöne", "Donny van de Beek", "Klaas Jan Huntelaar", "Kasper Dolberg", "Justin Kluivert"];
spelers.forEach(function(key, value){
console.log("Ik ben " + key + " En ik ben de " + (value + 1) + "e speler.");
});
使用while循环,将 voetbalteam
用作迭代器,并在while循环内对其进行递增.
Using a while loop, use voetbalteam
as an iterator and increment it inside the while loop.
var voetbalteam = 0;
var spelers = ["Nobert Alblas", "Kostas Lamprou", "André Onana", "Benjamin van Leer", "Léon Bergsma", "Damil Dankerlui", "Matthijs de Ligt", "Mitchell Dijks", "Luis Orejuela", "Daley Sinkgraven", "Joël Veltman", "Nick Viergever", "Max Wöber", "Deyovaisio Zeefuik", "Frenkie de Jong", "Siem de Jong", "Carel Eiting", "Noussair Mazaoui", "Lasse Schöne", "Donny van de Beek", "Klaas Jan Huntelaar", "Kasper Dolberg", "Justin Kluivert"];
while (voetbalteam < spelers.length){
console.log("Ik ben " + spelers[voetbalteam] + " En ik ben de " + (voetbalteam + 1) + "e speler.");
voetbalteam ++;
}
在编码时,请考虑使用 console.log()
而不是 alert()
.
consider using console.log()
instead of alert()
when you are coding.
-
alert()
正在阻止 在非调试环境中, -
alert()
很难被抑制 -
控制台
通常可以很好地格式化对象并允许遍历他们 - 日志记录语句通常具有交互式的代码指针,该代码可以发布的日志记录声明
- 您一次不能查看多个
alert()
消息 -
控制台
可以采用直观的格式设置不同的日志记录级别
alert()
is blockingalert()
cannot be easily suppressed in non-debug environmentconsole
typically formats your objects nicely and allows to traversethem- logging statements often have an interactive pointer to code whichissued logging statement
- you cannot look at more than one
alert()
message at a time consoles
can have different logging levels with intuitive formatting
这篇关于用while循环通知数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!