【发布时间】:2016-04-07 17:05:27
【问题描述】:
我是 JavaScript 新手,我正在尝试编写一个简单的函数来使用 while 语句打印数组元素,但最后我得到了一个额外的未定义值。任何帮助将不胜感激
代码是:
var a = [1, 3, 6, 78, 87];
function printArray(a) {
if (a.length == 0) {
document.write("the array is empty");
} else {
var i = 0;
do {
document.write("the " + i + "element of the array is " + a[i] + "</br>");
}
while (++i < a.length);
}
}
document.write(printArray(a) + "</br>");
输出是:
the 0element of the array is 1
the 1element of the array is 3
the 2element of the array is 6
the 3element of the array is 78
the 4element of the array is 87
undefined
我如何获得未定义的值?我跳过任何索引吗?提前致谢!
【问题讨论】:
标签: javascript