1

I'm trying to write a function that returns the index of an object where the value is less than 100. It currently returns -1 indicating that none of the objects meet that criteria when in fact I know that one does. I imagine it's something simple that I am just spacing out on. Can anyone offer a suggestion? Thanks

var distance = [ 16996.054413094975,
  102.48330298955042,
  8930.89370465407,
  10856.832465874579,
  6820.160641562082,
  114.07012348350867,
  8730.587245583654,
  325.9682182750614,
  6247.132833741246,
  6528.189432909801,
  10856.170817339278,
  8036.961700844721,
  98.66650647724741,
  16265.209545673928,
  16266.769167269893,
  715.0404901364141,
  7121.899289338758 ];


var addresses = function(){
      for (var i = 0; i <= distance.length; i ++)
      var indexDistance = distance.indexOf((i) <= 100);
      console.log(indexDistance);
    };

addresses();
6
  • Why are you giving a boolean value to indexof when you're looking for numbers? indexof isn't a macro. It doesn't work like that. Commented Sep 21, 2017 at 15:26
  • You're probably better off putting the loop content in braces to avoid a potential "goto fail" bug. Commented Sep 21, 2017 at 15:31
  • Thanks @Carcigenicate I guess now I know why it doesn't work. Boolean's do work on numbers in certain cases though as in NaN and == right? I'm pretty new to coding and am open to any advice, criticism, and direction so I appreciate the answer. Commented Sep 21, 2017 at 15:36
  • @BobDeckard Check my answer. There's an easy solution. Commented Sep 21, 2017 at 15:36
  • @Andrew Morton thanks, I'm going to expose my ignorance to try to cure it. What is the goto fail bug? Also when you say content braces? Not sure exactly what you are referring or where I would put the Commented Sep 21, 2017 at 15:46

2 Answers 2

2

This function already exists. It's called findIndex.

distance.findIndex(i => i <= 100)
Sign up to request clarification or add additional context in comments.

Comments

0

indexOf returns the first index of the argument you give it. You're giving it an inequality, which evaluates to a boolean value before being passed. That means you're basically telling it to search for boolean values in an array that contains only numbers. That's why it's returning -1; your list doesn't contain any literal true or false values.

I believe you're looking for filter:

// Find all numbers <= 100
var smallest =
    filter(n => n <= 100, distance)

// Get their indices
var indices =
    map(n => distance.indexOf(n), smallest);

This will find all the indices of all the numbers. If you only want the index of the first number less than 100, you can simplify it a bit:

// Find all numbers less than 100
//  then grab the first
var smallest =
    filter(n => n <= 100, distance)[0]

// Get the index
var index =
    distance.indexOf(smallest). 

6 Comments

Ok I simplified my array to make it easier to see what's going on and tried this.
var distance = [ 100, 101, 100.5, 9 ]; var smallest = distance.filter(n => n <= 100, distance)[0] // Get the index var index = distance.indexOf(smallest) }; addresses();
@BobDeckard Note, this is the "manual" way. torazaburo mentions a simpler way to achieve almost the same thing.
and then I changed it to return < 100 but it only gives me the indexOf the first element that meets the criteria.
@BobDeckard You only have 1 element that's less than 100, so that's what it gives you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.