Hey. My .push does not work from the if statement. What should I do to make it work? Code:
function getIndexToIns(arr, num) {
var arrInitial=arr.slice.call(arguments);
var arrFrom=arrInitial[0].sort(function(a, b) {
return a - b;
});
var final=[]; //empty array where x should go
var toDelete=arrInitial.slice(1);
for (x=0;x<arrFrom.length;x++) {
if (arrFrom[x] >= toDelete[0]) {
final.push (x); //push which supposed to be working
}
}
return final;
}
getIndexToIns([2, 5, 10], 15);
It does work. But arrFrom[x] >= toDelete[0] is never true for the arguments you pass to the function. Try getIndexToIns([2, 5, 16], 15); to see that push() is working.
So most likely the code is doing something else than you expect it to do. Is this a FCC challenge?
Tnx. Yep, it’s Where do I belong challenge. Strange things, but when I had “return x” instead of “final.push(x)”, FCC told me that almost everything was fine. A glitch?
No, that is almost correct. To fix your current code to return the same as just return x is using return final[0] instead of final , since final is an array and you have to return a number.
The only case that isn’t working is when none of the numbers in the array is larger than the number that must be inserted.