Hi guys, I was just not passing the last two tests so I noticed that the reason was I was placing else {return arr.length }
as part of the if part function like so . Why is it in the outerlayer?
for (let i = 0; i < arr.length; i++) {
if (num <= arr[i]) {
return i;
} else {return arr.length
}
}
}
**Your code so far**
function getIndexToIns(arr, num) {
var sortedArr = arr.sort();
function compareNumbers (a,b) {
return a - b;
}
sortedArr.sort(compareNumbers);
for (let i = 0; i < arr.length; i++) {
if (num <= arr[i]) {
return i;
}
}
return arr.length;
}
console.log(getIndexToIns([2, 5, 10], 15));
// re-order arr
// loop through arr to find at what index is num smaller than arr[i]
// return the index as a result
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36
.
Challenge: Where do I Belong
Link to the challenge: