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 Arrays Loop Through Arrays Locate and Join Array Elements

Video Ends with Dead Code

The code at the end of this video with a few of my comments and I use ">" to highlight the 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;

/// deskcheck cookies

if ( !search || isNaN(search) ) { message = <strong>In Stock:</strong> ${inStock.join(', ')}; } else if ( inStock.includes(search.toLowerCase() ) ) { message = Yes, we have <strong>${search.toLowerCase()}</strong> >in stock. It's #${inStock.indexOf(search) + 1} on the list; } else { message = No, we do not have <strong>${search.toLowerCase}</strong> >in stock.; } // End IF

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

The "isNaN" thing is my own doing but that is not the point and should not effect the point I am attempting to makeโ€”

The point is the "!search" when it DOES run, with search is falsey, the "else" block will never run. This is because !search also means a legit string but one which is not inside the array. Therefore, this "else" block is dead code and really the "elseif" should become the "else".

Am I missing something?

2 Answers

if (search is undefined) {
  show what's in stock
} else if (what you search for is found from stock) {
  show message and information about the item found in stock
}ย else {
// (all other possible scenarios
// (in this case the scenario when the code reaches else is if
// search is defined but nothing found from stock with the search word)
  show message that the item was not found in stock
}

so nope, it is not dead code.

Hello Ms. Ruokokowski

Thanks for the reply.

I appreciated you explaining each line of code.

I believe where I am short-circuiting in my synapses is that the variable "!search" also includes those variables that are defined, or truthy, but not found in the list given in the inStock array. In other words: "bananas" would seem to me also be !search, but I guess after reading your explanation, I really should think "bananas" should be โ‰  "search".

Still hard for me: "bananas" โ‰  "search" still seems synonymous to "!search" since I see both as falsey.


I guess my isNaN is redundant also since this value is included in the list of falsely values.

Ignacio Rocha
Ignacio Rocha
7,462 Points

I hope that I might catch your doubt...

if (!search) {
 /** this block will evaluate if !search is falsey, it will be if the value of
  * the prompt is null and you get that value if
  * you click on cancel or type a empty message.
  */
  message=`In stock: ${inStock.join(',')}`;
} else if (inStock.includes(search.toLowerCase().trim())) {
  /**
   * If you type even one letter or number the prompt value 
   * will be true and therefore you will enter in this block.
   */
  message=`the item ${search} is number 
  #${inStock.indexOf(search.toLowerCase().trim())+1} in the list`
} else {
 /**
  * If the word or number that you enter is not in the array 
  * you will enter here and display the message.
  */
  message=`Sorry we don't have ${search}`
}

Note: the 'trim()' was part of my solution because I hate empty spaces after or before the prompt messages.