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.