# Where-do-i-belong Help

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

2 Likes

Understood, thank you.

I figured out my problem.

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) // I was adding curly-brackets{} here when it was unnecessary, causing the code to break. I am not sure exactly why though

return i;
}
return sorted.length;
}

getIndexToIns([5, 3, 20, 3], 5);
``````

I was adding curly brackets after my IF statement which was causing the break in my code to happen. Although I dont exactly know the specifics of why, It would be nice to have an explanation from someone that does.

``````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;
}
}
return sorted.length;
}
``````

This works perfectly fine adding curly brackets after your `if` statement, and makes it more readable. Not sure what was messing up. If i had to guess maybe you accidentally put `return sorted.length` inside the `for` loop? That would definitely cause some problems.

Your problem in your first code was the `else if` logic. What happens when `num` is 100 and the first number in the sorted array is 1?

The `else if` being the only one up there was kind of a mislead, I had `else if` `if`, and I also tried just plain `else`, but something was causing that same break every-time, after removing the brackets it was fine. I was definitely messing up somewhere in the code, at this point though, it’ll probably remain a mystery. At the point of adding `else if` I was just trying anything to see what was causing the problems.