Where do I belong algorithm not passing, right answer

Hi guys, I’m baffled as to why my code wont pass even though it is logging out the right answer.

I’m using a for in loop which wont pass. But when I use a normal for loop, to achieve the same exact answer, it passes. Any idea why?

Your code so far

function getIndexToIns(arr, num) {
  const target = num;

  for (var i in arr) {
    if (arr[i] === target) {
      return i;

function sortNumber(a,b) {
    return a - b;

getIndexToIns([2, 20, 10], 19);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:

Inside the first line of your for loop, put the following console.log statement:

console.log(typeof i);

You will probably be surprised at what you see. When you write (for i in arr), i is a property of an object (JavaScript arrays are really just objects). Object properties are strings, so you are returning a string value instead of a numeric value which is what the challenge is expecting.

1 Like

Oh wow lol. Such a simple yet sneaky oversight on my part. Thank you very much Randell!