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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Wojciech Kwiatkowski](https://secure.gravatar.com/avatar/6a7749c096bb5f65ee9e317b802a15f8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Wojciech Kwiatkowski
5,856 PointstoLowerCase() method makes problem
Everything with my script is ok until I add the toLowerCase() method. Why is it a problem? Am I writing it wrong? When I add toLowerCase() in line 4, the script does not open and DevTools shows me that comment: Uncaught TypeError: Cannot read property 'toLowerCase' of undefined.
What is wrong?
Below is my script. Please help.
var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];
var search;
search = search.toLowerCase();
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 (', ') )
} else {
if (inStock.indexOf (search) > -1) {
print ("Yes, we have " +search +" in the store.");
} else {
print ("No, we do not have " +search +" in the store.");
}
}
}
2 Answers
![KRIS NIKOLAISEN](https://secure.gravatar.com/avatar/65d74a723997b41a80d2d909c6e06960?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
KRIS NIKOLAISEN
54,969 PointsWhere you have it now search has been declared, but nothing has been assigned to the variable yet which is causing the error.
search = search.toLowerCase()
should be moved into the loop just after the prompt so that you update the current value of search.
![Wojciech Kwiatkowski](https://secure.gravatar.com/avatar/6a7749c096bb5f65ee9e317b802a15f8?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Wojciech Kwiatkowski
5,856 PointsThank You! That was the clue!