2014-01-12 92 views
0

你好我試圖創建一個應用程序,接受來自用戶的輸入,然後將其發送到數組打印到屏幕上。我無法讓數組正確循環,並打印到屏幕上。這是我到目前爲止有: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."); 

     } 
} 
+0

歡迎來到StackOverflow!如果您能提及目前的產出和您期望看到的結果,那麼回答您的問題會更容易一些? – kukido

回答

0

您應該使用其他變種的提示雜貨:

var grocery = null; //Add this var 
while (grocery != 'q') { 
    grocery = prompt("Enter an item to add to the grocery list (enter \‘q\’ to quit):", null); 

    if ((grocery !== null) && (grocery != "q")) { 
     myGroceries.push(grocery); 
     canExit = true; 
    } 
} 

在你的舊代碼,您使用相同的變種myGroceries爲您的陣列和項目,所以它會被提示字符串覆蓋。

在您的舊代碼中,您對數組和項目使用了相同的var myGroceries,因此它被提示字符串覆蓋。

編輯雖然以上是正確的,這將是清潔IMO

var grocery; 
do{ 
    grocery = prompt("Enter an item to add to the grocery list (enter \‘q\’ to quit):", null); 
}while (grocery != 'q' && grocery !== null); 
myGroceries.push(grocery); 

乾杯

+0

這工作完美。感謝您的快速回復! – McFly

+0

歡迎:)。你能否將我的答案標記爲已接受? –

0

你的功能

function printGroceries() { 

不接受任何參數,而你傳遞雜貨它。它應該是

function printGroceries(myGroceries) { 

,並使用不同的變量提示

myprompt = prompt("Enter an item to add to the grocery list (enter \‘q\ 
+0

我試過一次,但沒有奏效。也許我沒有保存我的更改。 – McFly

0

很多事情是你的代碼錯誤(重複的變量,局部變量未定義的引用,等等)。嘗試

function getGroceries() { 

     var canExit = false; 
     var myGroceries = []; 
     var myGroceriesPrompt = prompt("Enter an item to add to the grocery list (enter \‘q\’ to quit):", null); 

      if ((myGroceriesPrompt !== null) && (myGroceriesPrompt != "q")) { 
       myGroceries.push(myGroceriesPrompt); 
       canExit = true; 
      } 
     return myGroceries; 

    } 

    function printGroceries(groceries) { 

     if (groceries.length > 0) { 
      document.write("Here’s your grocery list:" + groceries.join("<br></br>")); 

     } else { 
      document.write("Sorry, your list is empty."); 

     } 
    } 
    var groceries = getGroceries(); 
    printGroceries(groceries); 

myGroceries已經定義,您試圖訪問它,雖然你試圖訪問雜貨(兩次)。此外,您還嘗試在定義之前調用函數。

DEMO

0

我得到它的工作像它應該。我不得不解決很多錯誤。包括執行具有相同變量的不同方法的兩個不同事物。語句放置錯誤。還有其他的事情我忘記了。我還將document.write更改爲console.log。不建議使用document.write。但是如果您堅持使用它,只需將console.logs更改爲document.write。

var grocerieitem; 
var myGroceries = []; 

function getGroceries() { 

     grocerieitem = prompt("Enter an item to add to the grocery list (enter q to quit)"); 

     if ((grocerieitem !== null) && (grocerieitem != "q")) { 
      myGroceries.push(grocerieitem); 
      getGroceries(); 

     } 
     else { 
     printGroceries(myGroceries); 
     } 
} 

function printGroceries() { 

    if (myGroceries.length > 0) { 
     console.log("Here’s your grocery list:<br><br>" + myGroceries.join("<br><br>")); 

     } else { 
      console.log("Sorry, your list is empty."); 

     } 
} 

getGroceries();