我想从提示中获取用户的输入,将该输入存储在一个数组中,然后显示它。我只想使用for
循环从用户那里获取 10 个输入。我已经尝试过do-while
,如下所示。
var givenNames = new Array();
var pattern = /[\w\d]{1,}/ig;
do{
var name = prompt("Enter some names. Only letters and digits are accepted!\nEntering an empty field stops asking","");
if(name && name.match(pattern)){givenNames.push(name);}
}
while(name != "");
function displayNames(){
if(givenNames.length > 0){
document.getElementById("list").innerHTML = "<span style='color:Navy;font- weight:bold;'>Given names are:<\/span><br><br>" + givenNames.join("<br><br>");
} else {
document.getElementById("list").innerHTML = "<span style='color:Navy;font-weight:bold;'>Nothing has been given!<\/span>";
}
}
如何用for
循环替换它?