Where do I belong? why do we return arr.length?

Hi - I was below is my code but i do have a couple of question.
(1) on the for loop, why do i have to return “i” instead of “arr[i]”. Is it because the question asks for the index number and not the Value on a specific index?
(2) I’ve seen responses on this same exercise, returning “arr. length”? Why do we need length? I don’t think we’re being asked for the length of the array. Any guidance is greatly appreciated!

  **Your code so far**

function getIndexToIns(arr, num) {
arr.sort(function(a,b){
  return a-b;
});
console.log(arr);
for(var i =0; i < arr.length; i++){
  if(arr[i]>=num){
    console.log(arr[i]);
    return i;//why not arr[i];
  }
}
console.log(i);
return i;// why can't it be arr[i];
}
getIndexToIns([2, 5, 10], 15);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Where do I Belong

Link to the challenge:

why do i have to return “i” instead of “arr[i]”. Is it because the question asks for the index number and not the Value on a specific index?

Yes. The purpose of the function is to return an index. You could return the value, but then it would be a different function with a different purpose, and with a different name.

I’ve seen responses on this same exercise, returning “arr. length”?

It depends on the code. There may be situations where you return the length, which is basically the index of “one beyond” that array. Depending on how you’re using that array, what you’ve done to it, that may be a workable solution.

1 Like

thanks. I’m sorry. I’m still not clear about arr.length for this particular instance. i saw one of the solutions to be similar to my code, but the last return before we close the function had ‘return arr.length’
is there something I’m not seeing?

1 Like

It would help to see the solution you’re talking about, but here’s an example where you’d return the length:

Example
function getIndexToIns(arr, num) {
  const onlyTheSmallerNums = arr.filter(n => n < num);
  return onlyTheSmallerNums.length;
}

This function doesn’t bother sorting the array first, it filters instead. The filter returns an array with only the numbers that are smaller than num, so it changes the length of the array. The order of those numbers doesn’t matter, but if there are 3 items in there, you know that they’ll have indexes 0,1,2 , and your num needs to be inserted at index 3 (= the length).

2 Likes

The reason for returning arr.length is only in one particular case: if the inserted element is greater than anything currently in the array, it should be given the index one greater than the last index in the array.

Look at the given code:

getIndexToIns([2, 5, 10], 15);

The array itself contains three elements, so currently the final index (your last i value) is 2 - arr[0]==2, arr[1]==5 and arr[2]==10. So to insert 15, we need to go beyond that last index…by one.

But what’s the arr.length? Also, after the loop, you’re logging i - what does that show?

1 Like

Thank you! After the loop, I was logging i and showed the number 3. The exercise worked with both arr.length and just returning i after the loop. I just couldn’t understand why “length” was needed.

1 Like

You know, you’re absolutely right about i - i hadn’t noticed it was a var rather than a let. So the loop ends when i is 3, as i is no longer less than arr.length. I missed that lol

So yes, you’re right - returning i would be exactly the same as arr.length in this case. Had it read let i=0 though, it would be a different story.

And yes, all the exercise wants is where to insert the value (what’s the index) rather than what’s currently at that index.

In fact, just for fun, try making it read let i=0; And see what happens. :grin:

2 Likes

Thanks so much. I didn’t even think of the difference between var and let here… interesting. Thanks a lot!!!

1 Like

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