Tell us what’s happening:
I tried to solve this problem using the code below but I am not able to pass some test cases, which are:
-
getIndexToIns([10, 20, 30, 40, 50], 30)
should return2
. -
getIndexToIns([5, 3, 20, 3], 5)
should return2
. -
getIndexToIns([], 1)
should return0
. -
getIndexToIns([], 1)
should return a number.
Please tell me what do I need to change in my code to pass the test cases above.
Your code so far
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort();
for (var i = 0; i <= arr.length-1; i++) {
if(arr[i] > num) {
var val = i-1;
if(arr[i-1]<num) {
val = i;
}
if(arr = num) {
val = i;
}
}
if(num > arr[arr.length-1]) {
val = arr.length;
}
}
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/75.0.3770.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong/