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

Lee Howard
Lee Howard
6,621 Points

Whats the purpose for the ( search ) > -1?

I'm just really confused as to why he uses it. Dave mentions that if the value is not in the array then the interpreter returns the value -1 and if the index0f is greater than -1 then the value is in the array..

Why are we even checking the value? I don't see any number being printed to the page. I'm just really confused as to why it's even in there!!

4 Answers

Steven Parker
Steven Parker
229,732 Points

You didn't provide a link to the video, so I can't quote directly from it, but clearly the purpose of the test is to determine if the item that was searched for was actually found in the array. It might not be important to the program to print anything at this point, but the code in the block following this test is quite likely dependent on having found the term in the array.

To facilitate the most complete and accurate answers, always provide a link to the course page.

Steven Parker
Steven Parker
229,732 Points

So, to understand the purpose for the test we need to look at a bit more of the code from the video:

search.js
    if ( inStock.indexOf(search) > -1 ) {
      print( 'Yes, we have ' + search + ' in the store.');
    } else {
      print( search + ' is not in stock.'); 
    }

As I said, the test determines if the item is found in the array. From the additional code you can see that when it is found, the program will print "Yes, we have name_of_item in the store.".

When it is not found, the "else" clause will be executed instead and print "name_of_item is not in stock.".

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I wish I knew specifically to which video you were referring but let's try my own made-up example. What if I had a list of names of friends that I invited to a party. Let's say it's a pretty big and expensive party. Anyone not on that list is not allowed admittance. I've hired someone to watch the entry point to the party to make sure no one who isn't allowed comes in.

If I had an array with the lists of names of people that were allowed in and did a search against that array, if it returned a -1 I would know that they are not on the list and not allowed in. If the search shows any other number, they are allowed in.

Hope this helps! :sparkles:

Lee Howard
Lee Howard
6,621 Points

Sorry guys! I thought because I asked a question while watching the video it would of included it in the message!

https://teamtreehouse.com/library/useful-array-methods - here's the video I'm talking about. I hope that enables you to help me now :) Thank you!

Steven Parker
Steven Parker
229,732 Points

That helps! I added a comment to complete my original answer.

Lee Howard
Lee Howard
6,621 Points

Thanks Steven! I actually understand what you mentioned already. It's the " > -1 " that is confusing me. :/ :????

Steven Parker
Steven Parker
229,732 Points

The expression "> -1" creates a test that asks the question: "Is it greater than negative 1?".

If the indexOf function finds the searched item, it will return the index number of the array element where the item was found. And array indices start at 0 and go up. But if the function does not find the item, it returns -1 instead.

So by testing if the returned value is greater than -1, the test result will be true if the item was found, and false if the item was not.