Check an Element With indexOf: passing the test but not sure the answer is right

Tell us what’s happening:

I passed the test but I think this is wrong

Your code so far


function quickCheck(arr, elem) {
  // change code below this line

  let x = arr.indexOf(elem);
  console.log(x)
  if (x === -1){
    return false;
  } else {
    return true;
  }

  // change code above this line
}

// change code here to test different cases:
console.log([true, false, false], undefined);//returns a copy of the arr

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/check-for-the-presence-of-an-element-with-indexof

I think this is right.
Why do you think it is false?

You are not logging the function return, you are logging the two arguments that would/should have been passed to the function.

You are doing this:
console.log([true, false, false], undefined);

Not this:
quickCheck([true, false, false], undefined)

Good question: because is not returning true or false.

Look this:

  // change code below this line

  let x = arr.indexOf(elem);
  console.log(x)
  if (x === -1){
    return false;
  } else {
    return true;
  }

  // change code above this line
}

// change code here to test different cases:
quickCheck([true, false, false], undefined); //return -1, BUT not false

That is the reason why you see -1. Remove this line. and try
console.log(quickCheck([true, false, false], undefined))

1 Like

It is both logging -1 and returning false.

You have to be careful about how you use the challenge console it isn’t the same as the browser.

I would suggest always running the code in the browser console. If you have VS Code I’d also suggest checking out the Quokka extension.

1 Like