Tell us what’s happening: Hi! I’m trying to complete one of the challenges (Basic Algorithm Scripting: Where do I belong), and I have all conditions covered except where arr is an empty array []. As my code shows, I try to check for this first with an if statement, but instead the function returns “undefined” instead of 0. Every other conditions seems to work fine. I would appreciate any guidance for what I might be missing! I can think of some other ways to solve this challenge, but now I just really want to know what is wrong with what I have so far with the empty array check at the top. Thanks!
Your code so far
function getIndexToIns(arr, num) {
if (arr == []) {
return 0;
} else {
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (num > arr[arr.length-1]) {
return arr.length;
}
if (num <= arr[i]) {
return i;
}
}
}
}
getIndexToIns([], 1);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
.