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

Mustafa Başaran
Mustafa Başaran
28,046 Points

A very simple alternative solution without a while loop.

Greetings.. I tried the following JS script before watching the lecture to the end. It is quite basic stuff but it is still working. Have a beautiful day.


function lookFor() {
 search = prompt('Search for a product in the store.');

      if (search === 'quit') {
        print('Have a nice day.')
      }

      else if (search === 'list') { 
        var listIt = inStock.join(', ');
        print(listIt);
      }

      else if (inStock.indexOf(search) !== -1) {
        print('Yes, we have ' + inStock[inStock.indexOf(search)] + ' in the store.');
      }

    else {
      print('We do not have ' + search + ' in the store.');
    }
}

lookFor();

3 Answers

what are you looking for? i cant see a while loop in your code..?

Mustafa Başaran
Mustafa Başaran
28,046 Points

Thanks Shez. I am not asking anything particular. I just wanted to share a simpler version without a loop.

Michael Anetsberger
Michael Anetsberger
4,681 Points

I tested your code and found only one problem, it doesn't repeat if you searched for Milk for instance, the code ends after the first message is written and doesn't prompt the user for more input. Cool method though, but I think for this a loop is in order, either that or a lot of lines calling the function, although even a lot of lines calling the function doesn't work properly because it doesn't break away from the next function call if 'quit' is written.

You also probably should convert search .toLowerCase()