我是JavaScript新手,我正在嘗試編寫一個簡單的函數來使用while語句來打印數組元素,但我在最後得到了一個額外的未定義值。任何幫助將高度讚賞使用while循環在printArray函數中獲取額外的未定義值
代碼是:
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
我如何獲得未定義的值?我是否跳過任何索引?提前致謝!
感謝一噸....問題解決和解釋... –