你好我試圖創建一個應用程序,接受來自用戶的輸入,然後將其發送到數組打印到屏幕上。我無法讓數組正確循環,並打印到屏幕上。這是我到目前爲止有:JavaScript數組問題與打印結果
var groceries = getGroceries();
printGroceries(groceries);
function getGroceries() {
var canExit = false;
var myGroceries = new Array();
while (myGroceries != 'q') {
myGroceries = prompt("Enter an item to add to the grocery list (enter \‘q\’ to quit):", null);
if ((myGroceries !== null) && (myGroceries != "q")) {
myGroceries.push(myGroceries);
canExit = true;
}
}
return myGroceries;
}
function printGroceries() {
if (myGroceries.length > 0) {
document.write("Here’s your grocery list:<br><br>" + myGroceries.join("<br><br>"));
} else {
document.write("Sorry, your list is empty.");
}
}
歡迎來到StackOverflow!如果您能提及目前的產出和您期望看到的結果,那麼回答您的問題會更容易一些? – kukido