Why do we return arr.length?

Can someone explain why we return arr.length at the end of this code challenge? I understand returning the index, but in order to complete the challenge I had to insert “return arr.length” which I found in the “get help” for this challenge.

Challenge: Where do I Belong

It’s really hard to help with seeing the code you are talking about.

Also, it would be even better if I could see your code before you looked at the answers so I can show you what happens without that line.

Apologies, I thought this was linked to the challenge and solution in question.

Here is the solution answer provided:

``````function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);

for (let i = 0; i < arr.length; i++) {
if (arr[i] >= num)
return i;
}

return arr.length;
}
``````

Here was my code:

``````function getIndexToIns(arr, num) {

arr.sort((a, b) => a - b)

let index = 0;

for (let i = 0; i < arr.length; i++) {

if (num <= arr[i]){
index = i
}

}
return index
}
``````

When my attempt wouldn’t work, I just tried returning i. Eventually I had to look at the answer to pass. And I was having a hard time understanding why `arr.length` had to be returned.

``````function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);

for (let i = 0; i < arr.length; i++) {
if (arr[i] >= num)
return i;
}

return arr.length;
}
``````

the for loop will iterate through the array and compare each value with number to insert, and if find any value greater than or equal to the number to insert we insert at that position.

`[1,2,3,4,5,6,7] insert 5`

first compare 5 with 1 condition false
and 2 flase
3 false …
5 true . so insert at that position

now consider this scenario

`[1,2,3,4,5,6,7] insert 9`

first compare 5 with 1 condition false
and 2 flase
3 false …
7 false

we finished the iteration and the condition is not true for any of the elements. which means all the elements in the array is less than the number to insert. so the position of insertion is simply the last index which is equal to array.length, so just retun that. thats it

1 Like

When in doubt, `console.log()`!

``````function getIndexToIns(arr, num) {
arr.sort((a, b) => a - b);
// Note, I'm not wild about mutating inputs
// and you don't need to sort arr to get
// the code code to work if you're careful
console.log("checking arr =", arr);
console.log("with num =", num);

console.log();
let index = 0;
console.log("initial value");
console.log("index =", index);

console.log();
console.log("looping over array elements");
for (let i = 0; i < arr.length; i++) {
console.log("i =", i, ", arr[i] =", arr[i]);
if (num <= arr[i]) {
console.log("updating index");
console.log("index =", index);
index = i;
}
}
console.log("finished looping over array elements");

console.log();
console.log("final value");
console.log("index =", index);
return index
}

console.log("'Good' case:")
console.log("--------------------");
let index = getIndexToIns([20, 40], 30);
console.log("Actual: ", index, ", Expected: 1");

console.log("\n--------------------\n");
console.log("--------------------");
getIndexToIns([20, 40], 10);
console.log("Actual: ", index, ", Expected: 0");

console.log("\n--------------------\n");