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
Joseph Falconer
Courses Plus Student 15,887 PointsWhy don't the values get pushed to the 'bag' array?
Here is one for you good people:
I'm making a program in which the 'butler' (Jeeves) takes food requests from the user (prompt), goes to the supermarket, and returns what he was able to buy there.
In the first .js file below I create two arrays, 'shopStock' and 'shoppingList'. I then run a for loop to fill the array 'bag' with the values from 'shoppingList' which are present in 'shopStock'. This code works perfectly: the 'bag' array gets the available values from 'shopStock' and prints to the document.
However, when I try to pass values from a user-entered array (see second .js file further down), the 'bag' array doesn't get the values, and the console shows that 'bag' is empty. I don't understand why it doesn't work in the second .js file (further down).
var shopStock = ['eggs', 'bacon', 'onions', 'broccoli', 'jam', 'chicken', 'tea', 'coffee', 'milk', 'chocolate', 'wine', 'cheese', 'salmon']; var shoppingList = ['eggs', 'bacon', 'onions', 'broccoli', 'rice', 'cod', 'potatoes']; var bag = [];
function print (message) { document.write(message); }
for (var i = 0; i < shoppingList.length; i += 1) { if(shopStock.indexOf ( shoppingList[i] ) > -1) { bag.push(shoppingList[i]); } } print(bag.join(", "));
Here is the second .js file. Here I try to pass values through the for loop from 'shoppingList' as input from the user prompt. But now 'bag' doesn't receive the values:
var shopStock = ['eggs', 'bacon', 'onions', 'broccoli', 'jam', 'chicken', 'tea', 'coffee', 'milk', 'chocolate', 'wine', 'cheese', 'salmon']; var shoppingList = []; var bag = []; var html;
function print (message) { document.write(message) }
function getList () {
var requests = [];
var request;
var end;
do {
request = prompt("Please enter the item(s) you need. Enter 'nothing else' when you're finished");
if (request === 'nothing else') {
end = 'nothing else';
} else {
requests.push(request);
}
} while (request !== 'nothing else')
return requests;
}
shoppingList.push(getList ());
for (var i = 0; i < shoppingList.length; i += 1) { if(shopStock.indexOf ( shoppingList[i] ) > -1) { bag.push(shoppingList[i]); } } print(bag);
As I say, I've tested everything and all else seems to be running well. The 'bag' array just doesn't get the values from the for loop in the second .js file, even though The shoppingList array has the values...
Anyone know what I'm missing?
Thanks in advance!
1 Answer
Seth Kroger
56,416 PointsFirst, you cleared the list of any values in the 2nd script. Then this line here: shoppingList.push(getList ());. push() only puts a single item into the list. Since getList() returns a list, you're pushing the list as a whole into shoppingList, not the individual items. So shoppingList becomes a list containing a single list item, not a list of items. You should just use shoppingList = getList(); instead.
Joseph Falconer
Courses Plus Student 15,887 PointsJoseph Falconer
Courses Plus Student 15,887 PointsFixed, thank you!