这是我的代码。我已经尝试了很多东西,但是没有用。请不要给我一些复杂的代码,因为我只是javascript的中级程序员。因此,如果可以的话,请尝试使事情尽可能简单。感谢所有提供帮助的人。
function readNumberOfEntries() {
var entryNumber, numberOfEntries, a;
do {
numberOfEntries = Number(prompt("Enter Value Here"));
if (numberOfEntries < 2) {
alert("Error: Number is not greater than or equal to 2");
}
} while (numberOfEntries < 2);
for (entryNumber = 0; entryNumber < numberOfEntries; entryNumber++) {
a[entryNumber] = Number(prompt("Enter Value"));
a = new Array(numberOfEntries);
document.writeln(a);
}
}
最佳答案
代码的问题在于,在for循环的每次迭代中都将覆盖数组。这应该可以解决您的问题:
function readNumberOfEntries() {
var entryNumber, numberOfEntries, a;
do {
numberOfEntries = Number(prompt("Enter Value Here"));
if (numberOfEntries < 2) {
alert("Error: Number is not greater than or equal to 2");
}
} while (numberOfEntries < 2);
a = new Array(numberOfEntries);
for (entryNumber = 0; entryNumber < numberOfEntries; entryNumber++) {
a[entryNumber] = Number(prompt("Enter Value"));
}
document.writeln(a);
}