Code doesn't work in FCC challenge, but works in other editors

Hi everyone!
Trying to solve Where do I Belong Challenge in the Basic Algorithm Scripting section.

The code I have wrote perfectly works in this code editor: [https://eloquentjavascript.net/code/], but for some reason does not get accepted in the challenge.

Could anyone see what the problem could be?

function getIndexToIns(arr, num) {
  let place;
  for(var x in arr){
    if (arr[x]>num) {
    place=x;
    break;
    }  
  }
  return place;
}
getIndexToIns([40, 60], 50);

Hi @s4ek1389, welcome to the forum.
The challange states

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Your code has the problem that it’s not odering the array before doing the check.
It would work in the case the array is already sorted, but there are cases in the tests where it’s not.

Also if none of the elements in the array are greater than the given number, what is getting returned is undefined. You might want to return something also in this case (eg. look at the test getIndexToIns([2, 5, 10], 15) should return 3.).

Happy coding!

@simonebogni, thank you for your tips. I’ve added a sort method before my main code, but even after I sort the array, my solution still doesn’t pass the test, although in the other editor I get the exact output that is required by this challenge. Please have a look at the screenshots below. Any idea why this could be happening?

it would really help in helping you if you would post your code instead of a screenshot and the link to the challenge

Sure.

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

function getIndexToIns(arr, num) {
  let place;
  arr.sort(function(a,b){return a-b});
  if (arr.length==0){place=arr.length};
  for(var x in arr){
    if (arr[x]>=num) {
    place=x;
    break;
    }
    else {
    place=arr.length;
    }
  }
  return place;
  
}
getIndexToIns([40, 60], 50);

I found the part, that doesn’t work: It’s the for..in loop. If I change it for a an ordinary for loop, it works.

But I don’t understand why. Both loops iterate through the array, why does the code work differently then?

Can anyone explain this part?

Here is the code, that worked:

function getIndexToIns(arr, num) {
  var place=0;
  arr.sort(function(a,b){return a-b});
  if (arr.length==0){
    place=arr.length;
    }
  for(var x=0;x<=arr.length;x++){
     
    if (arr[x]>=num) {  
    place=x; 
    break;
    }
    else {
    place=arr.length;
    }
    
  }
  return place; 
}
getIndexToIns([40, 60], 50);

In the for…in loop x is a string. Converting it to a number should work.

place = Number(x);

Also just FYI, you may not want to use for..in to loop Arrays.

2 Likes

that was what was nagging me! awesome spot! I need to add typeof as debugging tool

1 Like

typeof is definitely handy. The browser console also shows the type in the return (42 vs “42” or whatever).

@lasjorg thanks a lot! Now it makes sense.