Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
erdragerdragsson
Courses Plus Student 5,887 PointsProblem with javascript array
I have an array with different food things, and then a prompt where you can choose one of these things at the time, and the i want to show them what they choosed in the "grocery()" function, but it only displays "done" as it was the last word i typed, is there anyway to take out the thing from the first array, when i type it in the prompt, and store it in a new array variable, and the display it after? I've tried everything i can.
Can anyone please help! i would appreciate it! :)
Heres the code
function blue() {
alert("You start walking on the blue road");
alert("You spot a grocery store at the side of the road.");
alert("You decide to walk inside, and buy yourself some food.")
alert("You open the door to the store, you hear a loud bell as you opened the door.")
alert("You notice the cashier turning her head towards you.")
alert("You take a bag, and nods politely, then walks in to the store.")
alert("Heres a list of all the things available in the store.")
var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'ham', 'carrot',
'broccoli', 'pizza', 'crackers', 'onion', 'banana', 'frozen dinner', 'cereals'];
var search = prompt(inStock + " " + "Choose wich things you wanna buy, type 'done' when you are done." + " type one thing at the time.");
while( inStock.indexOf( search ) > -1 ) {
alert("You grabbed a " + search + " and put it in the bag")
search = prompt(inStock + " " + "Choose wich things you wanna buy, type 'done' when you are done." + " type one thing at the time.")
if (search === "done") {
grocery();
}
}
function grocery() {
alert("You decided that its enough")
alert("In your bag you have " + search);
}
}
1 Answer
Manju Muthaiya
2,644 PointsHi Erdrag,
You can track the items chosen in a new array. Initialize a new empty array (say 'bag') before prompting the user. The variable 'search' will only hold the last item typed in by the user. You can add this item to the bag array with the 'push' method of arrays. (bag.push(search)) The next time user types in something, 'search' will be replaced with the new item's name.
So now, 'bag' will contain the list of all the items chosen by the user. You can pass this array to the grocery function to display the items.
Hope this helps.
erdragerdragsson
Courses Plus Student 5,887 Pointserdragerdragsson
Courses Plus Student 5,887 PointsYeah that solved it!
Thanks for the help!