【发布时间】:2018-01-16 20:14:31
【问题描述】:
这是我的第一篇文章。我正在尝试构建一个基本的 js 程序,该程序将提示用户“输入英雄以查看他们是否在数组中”。该程序按预期执行,除了我希望在我的第一个“do {”之后提示提示,以继续提示用户,直到用户输入“Q”(是的,我对此很陌生)。 相反,我的程序在英雄被发现后停止提示用户。 谢谢,莎拉。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
</script>
</head>
<body>
<script type="text/javascript">
//Program name: Review
//Purpose: Allow user to search through array for hero
// Author: Sarah
//DLM: 15 Jan 2017
var more; //continue looping?
var hero; // hero to search for in the array
var ES = ""; //Space
var BR = "<br/>";
var ARRAYSIZE = 4;
var found=false;
//array of superheroes
var superheroes = new Array("batman", "superman", "spiderman", "hulk");
//continue until user wants to quit
do{
hero = prompt("Enter a hero to see if they are in array.", ES);
//to lower case
hero.toLowerCase = hero;
// loop through heroes, checks if user entered hero is contained within
for (index=0; index < 4; index++){
if (superheroes[index].toLowerCase() == hero){
found=true;
break;
}
}
if(found){
document.write(superheroes[index] + " is in the array." + BR)
quit="Q";
}else{
quit = prompt("Hero not found. Do you want to continue? (Y for yes, Q to quit)", ES);
}
}while (quit.toUpperCase() != "Q");
</script>
</body>
</html>
【问题讨论】:
-
因为你在找到它的时候将它设置为Q.....所以如果你不想让它停止,那么你应该删除那条线......
-
顺便说一句,
hero.toLowerCase() = hero;需要()来调用该方法。 -
不应该也是
hero = hero.toLowerCase();吗?
标签: javascript html arrays loops do-loops