Mutations challenge is mutating me!

Tell us what’s happening:
I am stuck on the mutations challenge and have been for a while now!
The code is a boolean check of a two element array - are each of the elements of the second string found in the first. My code correctly returns all the test strings as true/ false - except one, the first one “hello, hey”.
For instance:
“Mary, Aarmy” - correctly returns true.
“hello, neo” - correctly returns false.
But…“hello, hey” should read false, but is returning true! I can’t work out why??? I have checked all the other posts I could find on the forum on this subject and done some google searches and made some changes, but it still won’t work on this one array!!
If anyone could please explain why this one instance isn’t returning correctly that would be great!
Thank- you.

Your code so far

function mutation(arr) {
arr[0] = arr[0].toLowerCase();
arr[1] = arr[1].toLowerCase().split('');
  
 for (var i = 0; i < arr[1].length; i++) { 
  if (arr[0].indexOf(arr[1][i]) === -1) { 
    return false;
   } else { 
     return true;
   }
 }
}


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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:
https://www.freecodecamp.org/challenges/mutations

Oh Doh! Solved!

I was so focused on the rest of the code that I didn’t realise the “return true” had crept up a bracket or two in the function! Watching out for tigers and getting bitten by mosquitoes instead!

Oh well - it is a good lesson :slight_smile:

Was just about to say that: you are returning way too early; you should return true only after finishing looping through the whole word.

The reason why
"Hello Hey" returns true is because the first letters ‘h’ is present without even looking at the remaining letters.

1 Like

Thanks Marmiz,

I had been working on this code for a while as I started out with a “!== -1 return true” scenario and you realise how that would that have turned out! All this is quite tricky for someone with no computer science background at all. Good to know you were on the case, much appreciated.

 *******Mutations (Solution)********

function mutation(arr) {

var x = arr[0].toLowerCase().split(’’);

var y = arr[1].toLowerCase().split(’’);

return y.every(function(val){

return x.includes(val) ;

});
}

Thanks ahsanwaseem, I shall study this solution, it is much more elegant than the one I came up with!

1 Like
function mutation(arr) {
  let arr1 = arr[1].toLowerCase().split(''),
      arr2 = arr[0].toLowerCase();      
  
  return arr1.every(el => arr2.indexOf(el) !== -1);
}

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

Ok, sorry. I didn’t knew that. I posted some solutions without blur. I will blur the code solutions from now. Thanks!

Well to be honest i hate the fact that i can no longer see solutions for the exercises. I learned a lot from solutions. Usually my solutions were basic, and when i looked and intermediate and advanced solutions , i understood that i had to learn advanced methods. I like the new website, i don’t like that there are no solutions anymore :(, you have to use ask the forum.

Thanks spzHades, I will also study this solution - so elegant.

1 Like

from how much time you learning to code???

Didn’t see this solution.

function mutation(arr) {

  var box1 = arr[0].toLowerCase().split("");
  var box2 = arr[1].toLowerCase().split("");
  
  for(let i = 0; i < box2.length; i++ ){
    var result = box1.includes(box2[i]);   
    if(result === false){
      break;
    }
  }

  return result;

}

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

I am having the same issue here when using the spread operator. How would I go about fixing my code?

function mutation(arr) {
  let arr1 = arr[0].toLowerCase().split("");
  let arr2 = arr[1].toLowerCase().split("");
console.log(...arr2);
  if (arr1.indexOf(...arr2) === -1) {
    return false;
  }
  return true;
}

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

Thanks for your guidance!

The problem with the your code is, it returns true if the first letter matches and false if it doesn’t. But it won’t loop through all elements in the second array.

Try using for loop and if condition inside it.

Please let me know if you still didn’t get it.

Thank you