If statement not returning correct value but works when i put it in a seperate line alone

Why does my if statement keep returning true even though I set the condition to == 99999? I checked what ----arr[0].indexOf(arr[1].charAt(0) equated to alone on a seperate line and it would return the values I was looking for.

function mutation(arr) {
  arr[0] = arr[0].toLowerCase();
  arr[1] = arr[1].toLowerCase();
  var True = 0;
  var False = 0;
  
  for (i = 0; i < arr[1].length; i++) {
    if (arr[0].indexOf(arr[1].charAt(0) == 99999)) {
      return true;
    } else {
      return false;
    }
  }
  if (True == arr[1].length) {
    return true;
  } else {
    return false;
  }
}

mutation(["hello", "neo"]);

If I’m not mistaken it’s because in your code condition in question:

arr[0].indexOf(arr[1].charAt(0) == 99999)

… always evaluates to -1, which is not a falsy value and therefore the if condition always evaluates as true. The indexOf method returns -1 if the value provided is not found in the reference array, and otherwise the index of its first occurrence in the array; when you change your code, also keep in mind that:

['a', 'b', 'c'].indexOf('a'); // retruns 0, which is falsy

Good luck!

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

what do you mean by -1 not being a falsy value?