Where do I belong JavaScript

Where do I belong JavaScript
0

#1

Tell us what’s happening:

Your code so far

function getIndexToIns(arr, num) {
  arr = Array.sort();
  
  for (var i = 0; i < arr.length; i++) {
    if (num <= arr[i]) {
      return arr[i][0];
    }
  }

  return arr.length;
}

getIndexToIns([40, 60], 50);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/where-do-i-belong


#2

What is your specific question related to this challenge?


#3

My code does not work for this challenge I don’t get why it’s wrong.


#4

The above line produces the error:

TypeError: Array.sort is not a function

Why? Because, Array is a reserved word in JavaScript which does not have a sort function. arr is an array which you could use the sort function on (see below):

arr.sort();

The above code would sort arr by first converting the elements to strings (which is not what you want). Also the sort is done in-place so it replaces the original value of arr with the sorted version. Review the sort documentation for how to sort numbers properly.

You will also find you have at least one other issue with your code once you fix the above issue. One is your use of:

return arr[i][0];

arr is a one-dimensional array, so you could reference arr[i] to get a particular number in arr, but arr[i][0] attempts to get the first element in a sub array of arr, but arr does not have any sub arrays. It only has numbers as elements.


#5

@RandellDawson Strike Again!!
da da daaa

Can’t wait to get to that part.