2014-01-12 90 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();