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

Jana Ryndin
Jana Ryndin
7,161 Points

Last action - searching for an 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.');
    }
  }
Ruven Koviar
Ruven Koviar
3,917 Points
<body>


<button onclick="myFunction()">Try it</button>

<script>

var List = ["apple","orange"]

function myFunction() {

while (true){

var search = prompt("hi").toLowerCase();

if(List.indexOf(search) == -1){

alert ("We dont have it")}

else(List.indexOf(search) > -1)

{

alert ("We have it")

}

}


}
</script>

</body>

this works

i think you had problem with .toLowerCase (see how i wrote it) and maybe you wanted to alert(which will send a massage from the browser) in the if and else , and not print.

edit this code as you want

2 Answers

Hi Jana,

You're missing the closing curly brace for your while loop. Maybe that was a copy/paste error?

The other issues is that you have a typo in the following code: (missing the 'r' in "search" in the print function call)

 if (inStock.indexOf( search ) > -1) {
      print( 'Yes, we have ' + seach + ' in the store');

It seems to be working fine when I tested it after making those 2 changes.

Let us know if it's still not working and any errors you might be getting in the console.

Jana Ryndin
Jana Ryndin
7,161 Points

Thank you so much that you took time to find that 'r'. I went back and forth several times and could not see it already. Now it works!