JavaScript JavaScript Arrays Loop Through Arrays Search for a Value in an Array

Esther AlQaisi
Esther AlQaisi
2,260 Points

Here's my code (I converted the search variable toLowerCase)

I'm surprised Guil didn't do this since he's done it before in previous videos! I feel smarter than the teacher... lol! (let me have my moment) I tried Guil's code and input "Pizza" (with capital P) into the prompt box, and it returned "Sorry, we don't currently have Pizza." So I thought to convert the search variable toLowerCase so it would always match, no matter the case of the letters. Here's my code:

const inStock = ['pizza', 'cookies', 'eggs', 'apples', 'milk', 'cheese', 'bread', 'lettuce', 'carrots', 'broccoli', 'potatoes', 'crackers', 'onions', 'tofu', 'limes', 'cucumbers'];

const search = prompt('Search for a product.'); let message;

if ( inStock.includes( search.toLowerCase() ) ) { message = Yes, we have <strong>${search.toLowerCase()}</strong>.; } else { message = Sorry, we don't currently have <strong>${search.toLowerCase()}</strong>.;


document.querySelector('main').innerHTML = <p>${message}</p>;

1 Answer

Steven Parker
Steven Parker
207,608 Points

There will often be opportunities to make the exercise code more elegant, as the examples are always focused on a particular point and not intended to be comprehensive solutions.

Making the search case-insensitive is a very practical enhancement, good thinking!   :+1:

But don't do this on the challenges, the automation is very picky about what it expects and anything extra can confuse it.   :see_no_evil: