JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Samantha Atkinson
Samantha Atkinson
Front End Web Development Techdegree Student 19,056 Points

Using uppercase for the first letter of each item in an Array

I changed all the first letters of all the items in my array to Upper case and when I tested the program, the response kept being " not in stock" (final else statement) even though I input items that were definitely in the array. When I changed all the array items to all lower case it worked.

Could someone tell me why I couldn't use Uppercase for the first letter of every array item, please?

2 Answers

Carrie Short
Carrie Short
3,529 Points

If you followed the instructions in the video you included the line

search = search.toLowerCase();

This transforms the stored answer from the prompt into all lowercase. You are then checking the lowercase prompt answer against your Capitalized array item using indexOf().

"indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator)."

Which will return false if their is a difference in capitalization.