Mutations true and false order

Hey,

I was wondering if anyone could help me by explaining why it matters which way round the ‘return true’ and ‘return false’ statements are?

For example this passes the tests:


function mutation(arr) {
  let constWord = arr[0].toLowerCase()
  let test = arr[1].toLowerCase()
  for(let i = 0; i < test.length; i++){
    if (constWord.indexOf(test[i]) < 0){
      return false;
    }
  }
  return true;
}

Whereas this doesn’t:


function mutation(arr) {
  let constWord = arr[0].toLowerCase()
  let test = arr[1].toLowerCase()
  for(let i = 0; i < test.length; i++){
    if (constWord.indexOf(test[i]) >= 0){
      return true;
    }
  }
  return false;
}

Thank you!

here, you are checking if the first letter is included, and if it is included returning true - but what about all the other letters?
and if the first is not included it check the second, and if this one is included, it returns true etc

it returns true if one of the letters is present, not if all of them

the other way around return false as soon as one letter is not included, but wait to check them all before returning true

1 Like