Where do I Belong not working

Code is failing
This works perfectly when logging each example but failing when compiling


function getIndexToIns(arr, num) {
for(var val in arr){
    if(arr[val]>=num){
      return ""+val;
    }
}
}
getIndexToIns([40, 60], 50);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Where do I Belong

Link to the challenge:

Minor quibble - JavaScript is not compiled. You don’t have a compilation failure :slight_smile:

Small question as I look at you code:

Why are you converting the return value into a string?

Ok, take a look at this:

function getIndexToIns(arr, num) {
  // Note: using let is recommended
  // instead of var
  for (var val in arr) {
    // The instructions say that this
    // function must return a number.
    // Are you returning a number?
    console.log(typeof(val));
    if (arr[val] >= num) {
      return val;
    }
  }
  // What are you returning if no value
  // is bigger than num?
}
console.log(getIndexToIns([10, 20, 30, 40, 50], 35));

Once you look at those few issues, you’ll be pretty close to passing all tests. The only thing you’ll have left is

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.