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

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

My Array Method isnt worker I followed everything he did in the video but it is still not working can you help me

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;





while(true){
  search= prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to 'exit'");
  search= search.toLowerCase();
  if(search==='quit')
  {
    break;

}else if (search ==='list'){
print( inStock.join(',') );
}else{
  if ( inStock.indexOf(search)> -1 ) {
    print("Yes, we have"+ search+"in the store.");
  }else{
    print(search+ "is not in stock");
  }
}
}

Can you post a link to the code challenge you are working on?

Jana Ryndin
Jana Ryndin
7,161 Points

Hi All, what is wrong with my code? The last rule - searching for an each item does not work. var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber']; var search;

function print(message) { document.write( '<p>' + message + '</p>'); }

while (true) { search = prompt("Search for a product in our store. Type 'list' to show all the produce and 'quit' to exit."); search = search.toLowerCase(); if ( search === 'quit') { break; } else if ( search === 'list' ) { print(inStock.join(', ')); } else { if (inStock.indexOf( search ) > -1) { print( 'Yes, we have ' + seach + ' in the store'); } else { print( search + ' is not in stock.'); } }

13 Answers

It looks like you're missing the print function from the video.

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;

function print(message) {
  document.write( '<p>' + message + '</p>');
}

while(true){
  search= prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to 'exit'");
  search= search.toLowerCase();
  if(search==='quit')
  {
    break;

}else if (search ==='list'){
print( inStock.join(',') );
}else{
  if ( inStock.indexOf(search)> -1 ) {
    print("Yes, we have"+ search+"in the store.");
  }else{
    print(search+ "is not in stock");
  }
}
}
John Simoneau
PLUS
John Simoneau
Courses Plus Student 8,105 Points

Yeah, I just got stuck in the same spot pretty much. This code WILL NOT WORK in my version of Safari at least. I've tried everything. It worked fine for the first part but once I added the if search is list part it refuses to do it. Nothing appears when I type the word list until I type the word exit. Then it will print what it's supposed to however many times I typed the word list...LOL.

In Chrome it works perfectly and with zero errors. Not sure why Safari hates it...

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;

function print(message) {
  document.write( '<p>' + message + '</p>');
}

while (true) {
  search = prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to exit");
  if ( search === 'quit') {
    break;
  } else if ( search === 'list' ) {
    print( inStock.join(', ') );    
  }
}

One problem I see is you are using print() instead of console.log()

I also changed a nested if block around so that it was not nested and a little easier to read but that's a stylistic choice.

Fixed below:

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;

while(true){
  search = prompt("Search for a product in our store. Type 'list' to show all of the produce and 'quit' to 'exit'");
  search = search.toLowerCase();
  if(search === 'quit'){
    break;
  }else if (search === 'list'){
    console.log( inStock.join(', ') );
  }else if (inStock.indexOf(search) > -1) {
      console.log("Yes, we have" + search + " in the store.");
  }else{
    console.log(search + "is not in stock");
  }
}

In the Useful Array Method video he uses print because he printed it to the the web page
console log just print it in the console in the lesson he printed on a webpage The video name Useful Array Method

it stop working after the break code after it not working

Ahh my mistake then I think I'm looking at a different code challenge than you are. Can you post a link to the challenge?

I try the code and it still doesnt work when you enter list it is suppose to show the whole list and we you select and item in the array it suppose to display whether or not they have the item in stock

i think the print function was for the one of the previous exercise put i try it anyway and it still did not work

I try the code and it still doesnt work when you enter list it is suppose to show the whole list and we you select and item in the array it suppose to display whether or not they have the item in stock

i think the print function was for the one of the previous exercise put i try it anyway and it still did not work

Are you getting any errors? If so what are they?

The print function definitely needs to be there otherwise the default print function in javascipt actually tries to send the webpage to the printer.

Just doubled checked and the block of code I just posted seems to function correctly for me. List prints to the page everything in the store and inputting an item says if it is in stock or not.

No I dont get any errors put when I typing list it doent work or an item in a array it doesnt work

but ok in refers to the print function I dont quite how the message thing works in the function tho

No I dont get any errors put when I typing list it doent work or an item in a array it doesnt work

but ok in refers to the print function I dont quite how the message thing works in the function tho

Just to make sure, did you hit save on the .js file? Sometimes I forget and it drives me crazy why it's not working.

Sorry for the inconvenience ,but it appears there must have been something wrong with my web browser. It work perfectly in a different web browser. You were correct. However about that print function I do have a question about that. Why did he make message a property in the function. Did he just do that to help him remember that's were the message will be display and just added message or message actually does something important in the program. Because I ran it without the print function and the program work however the spacing was a little off put in the function he separate the lines with the paragraph symbol in html so that makes since. Can you assist me with this question?

function print(message) {
  document.write( '<p>' + message + '</p>');
}

In this function message is a parameter, he could have called message anything he wanted like x or turtle. In the first line of the function he is using document.write to write to the screen a <p> tag as a string and adds the message variable to the string with the + symbol and then a closing </p> tag.

When the function is called he passes a string as an argument. This string gets saved into the message variable and is used int he function.

Hope that clears things up!

Good evening everybody, Am having a similar problem that magically disappeared once I executed using chrome.

Why does safari refuse to display a document.write() output if we have another prompt coming up? the above challenge is our example.

Most of my problems seem to be sorted if I use chrome, but I still want to develop for safari.

John Simoneau
PLUS
John Simoneau
Courses Plus Student 8,105 Points

I still haven't found a solution to this document.write() issue on Safari. Be sure to post back if you or anyone figures it out :)