 # I have some doubts about Where Do I Belong in JS practice, please help me analyze it, thank you

**Stick to what you love and you’ll be happier **

I have some doubts about the topic Where Do I Belong in the JS basic algorithm script.
On the subject, my idea is, first of all, is to create a reproducible copy, and then through the sort () method to realize ascending, then by comparing the for loop using num and copy an array variable size, then by comparing the size, I found the greater than num copy an array variable, the variable, the index value of - 1 The debug of the console shows that there is no syntax error in the index return value. It is just that when the value is returned at the end, the correct content is not returned. I would like to know what knowledge points I am missing that cause this

Here’s the code I wrote

``````function getIndexToIns(arr, num) {
var localArray = arr.slice();
localArray.sort(function mysort(a,b){
return a-b;
})
for(let i=0; i< localArray.length; i++){
if(localArray[i] >num ){
return i-1;
}
}

return i-1;
}

getIndexToIns([40, 60], 50);

``````

@ieahleen

Hello @chucknorris,

Looking at your code - you are half way there - so do not dispair.

Running the test on your code you get the following:

``````// running tests
getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.
getIndexToIns([40, 60], 50) should return 1.
getIndexToIns([2, 20, 10], 19) should return 2.
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
``````

running those with console.log:

``````console.log(getIndexToIns([10, 20, 30, 40, 50], 35))// should return 3.
console.log(getIndexToIns([40, 60], 50))// should return 1.
console.log(getIndexToIns([2, 20, 10], 19))// should return 2.
console.log(getIndexToIns([2, 5, 10], 15))// should return 3.
console.log(getIndexToIns([2, 5, 10], 15))// should return a number.
console.log(getIndexToIns([], 1))// should return 0.
console.log(getIndexToIns([], 1))// should return a number.
``````

You get the following:

``````2
0
1
ReferenceError: i is not defined
``````

(side note: unless you use debugging tools to debug your javascript - console.log is your friend, use him as much as you can to figure out what values are)

looking at those and seeing what is required, it can be seen that the first 3 are one number out.

For the ReferenceError: i is not defined
Why would that be?
So looking at the code the variable i is defined within the for loop.
Where is it being used?
It is being used inside the for loop and outside the for loop.
So the error is due to the fact it is being used outside the for loop.
Lets change the return and return ‘outside for loop’

``````return 'outside for loop'
``````

now what is the resulting values from the console.log?

``````2
0
1
outside for loop
outside for loop
outside for loop
outside for loop
``````

So the top 3 are out by one.
And the bottom 4 are getting the return value outside the for loop.
Lets look at what is being sent in and analyse the outside for loop.

These two:

``````getIndexToIns([2, 5, 10], 15) should return 3.
getIndexToIns([2, 5, 10], 15) should return a number.
``````

the number 15 is bigger than what is in the array - conclude the for loop is not catering for numbers higher that the value in the array.

What about these two:

``````getIndexToIns([], 1) should return 0.
getIndexToIns([], 1) should return a number.
``````

Now no value in the array - so outside the for loop and there is nothing to loop through.
So empty arrays need to be catered for.

Hope that helps out.

Additionally the slice should not be required.

Thank you very much for your reply. I know what to do. Thank you!