我的第一个问题是我是否正确地将id“textOne”中的值输入到我的var a中?其次,我应该让我的while循环运行“而代表我想要打印的索引值的变量”大于或等于最小数组值。我的While循环条件是否正确?
我基本上希望根据1-5的用户输入在阵列中打印出正确数量的索引。
提前致谢。
<input type="button" value="Click" onclick="JobDuties()" />
to see my top
<input type="text" id="textOne" />
job duties here
<p id="answer"></p>
<script type="text/javascript">
function JobDuties()
{
var x = "";
var a = document.getElementById('textOne').value;
var b = a - 1;
myduties = new Array();
myduties[0] = "Saab";
myduties[1] = "Volvo";
myduties[2] = "BMW";
myduties[3] = "Toyota";
myduties[4] = "Ford";
}
while (a>=0)
{
x = x + myduties[b];
document.getElementById("answer").innerHTML=x;
}
</script>
答案 0 :(得分:0)
我假设您要打印出正确的数组值,而不是数组,因为没有多个。 PS&GT;如果你不这么说,人们不喜欢这里的作业问题,因为完全回答不会帮助你学习。我将部分回答。
以下代码:
var i = 1; //The variable you increment up to the use input variable a
while(i =< myduties.length){
if(i == a){ //if i and a are same then that position
//in the array will have the car type the user requested
myduties[i-1] //and set it to the DOM or document element similar to what you already had
}
i = i + 1; //Increment i towards the one less than the length of the array
}
不完美或检查但是应该为你提供一些指示。