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

Ioana Raluca Darie
Ioana Raluca Darie
3,540 Points

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

Help , why i receive this error ?

2 Answers

Hi Ioana,

Because you defined search as an empty var on line 2. In the while loop you define:

while ( true) {
    search = search.toLowerCase();

But, since search is empty, there is nothing to use the toLowerCase method on.

Elian

jobbol
seal-mask
.a{fill-rule:evenodd;}techdegree
jobbol
Full Stack JavaScript Techdegree Student 17,885 Points

What you have.

var search;
search = search.toLowerCase();
search = prompt( 'Search for a product in our store. Type "list" to show all the products in the stock and "quit" to exit! ' ) ;

This does.

  1. You first create search as a variable that points to nothing.
  2. You try to access nothing's toLowerCase method.
  3. Crash!

What you need

You need to first set search to your prompt and later convert that to lowercase.

var search;  
search = prompt( 'Search for a product in our store. Type "list" to show all the products in the stock and "quit" to exit! ' 
search = search.toLowerCase(); 

This will instead.

  1. Create search.
  2. Set search equal to a string.
  3. Access string's toLowerCase.