Mutations challenge, close but no cigar

Tell us what’s happening:
Thank you for your help.

With the current code im failing the first test of (hello, hey) should return false but i keep getting true.

However if i use result to assign true or false depending on the letter being checked in loop and then return result after the ‘for loop’ like below i pass the first one but fail three others that should be false but come back true

  function mutation(arr) {
  let checkAgainst = arr[0].toLowerCase().split('');
  let checkFor = arr[1].toLowerCase();
  let letters = checkFor.split('');
  let result;
  
  for(let idx = 0; idx < letters.length; idx ++){

    let letter = letters[idx];
    console.log(letter)
    if(checkAgainst.indexOf(letter) !== -1){
      result = true;
    } else{
      result = false;
    }
  }
  return result;
}
 
console.log(mutation(["hello", "hey"])); // false

console.log(mutation(["floor", "for"])); // true

console.log(mutation(["hello", "neo"])); // false

console.log(mutation(["voodoo", "no"])); // false

Your code so far


function mutation(arr) {
let checkAgainst = arr[0].toLowerCase().split('');
let checkFor = arr[1].toLowerCase();
let letters = checkFor.split('');
let result;

for(let idx = 0; idx < letters.length; idx ++){

  let letter = letters[idx];
  console.log(letter)
  if(checkAgainst.indexOf(letter) !== -1){
    return true;
  } else{
    return false;
  }
}
}


console.log(mutation(["hello", "hey"]));

Your browser information:

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

Challenge: Mutations

Link to the challenge:

    let letter = letters[idx];
    console.log(letter)
    if(checkAgainst.indexOf(letter) !== -1){
      result = true;
    } else{
      result = false;
    }

Since you are modifying the variable result with each index, this will only tell you if the last letter in the second array exists in the first string. If some letter in the middle is missing, that false value will be overwritten.

  let letter = letters[idx];
  console.log(letter)
  if(checkAgainst.indexOf(letter) !== -1){
    return true;
  } else{
    return false;
  }

In this block, the code exits on the very first letter and the result is dependent on that. If the first letter exists, it is true, otherwise false. It never goes past that.

You should check for if(checkAgainst.indexOf(letter) == -1) and if it is, return false. This way it will exit the loop if it doesn’t find a letter, but continue otherwise. Put a return true outside the loop at the end; if the loop goes through without returning false, it means all the letters exist in the first string.

You can do one of two things, both valid.

  1. Set result to true before the loop, and only flip it to false if needed; or
  2. Get rid of result, and if the letter if NOT found, simply exit three function early by returning false from the function inside the loop. If the loop goes through entirely, every letter was found, so you can just return true after the loop.

That cleared a lot for me thank you for the quick response.

I did both and worked perfectly, thank you for your help.

1 Like

I’m glad it worked out.

No problem! Happy coding~