Tell us what’s happening:
I tried to add spoiler tags, not sure if they were added correctly, this is my first post. Cannot figure out why the code breaks when trying to add anything after my first if statement, I fail these set of tests after messing with it.
Code:
function getIndexToIns(arr, num) {
let sorted = arr.sort((a, b) => a - b);
for(let i = 0;i < sorted.length;i++){
if(sorted[i] >= num){
return i
}else if (num > sorted[i]){
return sorted.length
}
}
}
getIndexToIns([5, 3, 20, 3], 5);
// running tests
getIndexToIns([10, 20, 30, 40, 50], 35)
should return
3
getIndexToIns([10, 20, 30, 40, 50], 30)
should return
2
getIndexToIns([40, 60], 50)
should return
1
getIndexToIns([5, 3, 20, 3], 5)
should return
2
getIndexToIns([2, 20, 10], 19)
should return
2
getIndexToIns(, 1)
should return
0
getIndexToIns(, 1)
should return a number. // tests completed
Taking my last else if statement out, causes an entirely different set of fails.
Code:
function getIndexToIns(arr, num) {
let sorted = arr.sort((a, b) => a - b);
for(let i = 0;i < sorted.length;i++){
if(sorted[i] >= num){
return i
}
}
}
getIndexToIns([5, 3, 20, 3], 5);
// running tests
getIndexToIns([2, 5, 10], 15)
should return
3
getIndexToIns([2, 5, 10], 15)
should return a number.
getIndexToIns(, 1)
should return
0
getIndexToIns(, 1)
should return a number.
// tests completed
I dont know what I need to do after that, it’s close. I know I need a statement for when the number is bigger than all the numbers in the array and it needs to display that position to the console, I’m not understanding why adding even a simple return length of the array if it doesn’t fit the first IF statement, breaks the entire first part of my code or adding an ELSE IF or just an ELSE produces a similar result. Been stuck on this for an hour, would really like a helping hand.
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0
Challenge: Where do I Belong
Link to the challenge: