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!

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

Phil Sta
Phil Sta
3,805 Points

None of my Array items are ever in stock - please help

Hi,

Would someone be so kind and have a look at my code below? Whatever I put in the prompt, the output is:

xxx is not in stock at the moment. Sorry

I just cannot figure out where the bug is.

Thanks a lot in advance!

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(message);
}

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all products. Type '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 stock." );
    }   else { 
        print ( search + " is not in stock at the moment. Sorry");
        }
    }
}

3 Answers

For your code below, it should be changed from this....

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all products. Type '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 stock." );
    }   else { 
        print ( search + " is not in stock at the moment. Sorry");
        }
    }
}

to this.

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all products. Type '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 stock." );
       } else {
          print ( search + " is not in stock at the moment. Sorry");
       }
    }
}

The error is in writing another else if statement. After the initial else if statement you made, it should be followed by an else statement. And then it should be followed by another if conditional statement if the user types in a specific item. I hope that helped!

You're finishing with 2 else statements which doesn't work. You need to turn the first one into an else if

Phil Sta
Phil Sta
3,805 Points

Hi ,

Thanks for your help -it still displays that no item is ever in stock

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(message);
}

while (true) {
    search = prompt("Search for a product in our store. Type 'list' to show all products. Type '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 stock." );
    }   else { 
        print ( search + " is not in stock at the moment. Sorry");
        }
    }
}

Any ideas?

Thanks!!