Need help with FreecodeCamp Mutations Algorithm

I have partially completed the following basic javascript algorithm called muttations. Here is my code;

function mutation(arr) {
  
let word = arr.map((val)=>{ 
return val.toLowerCase();
});
let word1 = word.slice(0,1);
let word2 = word.slice(1);
return word2.every(index => word1.includes(index))
}

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);//Should return true but returns false?

Thank you for any advice as to why above solution is not working.

word1 and word2 are arrays containing each a single string. You can’t use every() like that, it wouldn’t work

Maybe you need to transform them in arrays of letters?

Thanks for your help. I tried using split method but found that these requirements of mutations did not pass “mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]), mutation(["Alien", "line"]) should return true., mutation(["floor", "for"]) should return true.” . Here is my passing code using map to make all strings lower case.

function mutation(arr) {
  let word = arr.map(val => val.toLowerCase())

  let word1 = word[0];
  let word2 = word[1];

  for(let i = 0;i < word2.length;i++){
    if(!word1.includes(word2[i])){
      return false
    }
  }
  return true;
}
mutation(["hello", "hey"]);

I would like to add an update i found an solution online which shows that using split() method is possible.

function mutation(arr) {
  var a = arr[0].toLowerCase().split('');
  var b = arr[1].toLowerCase().split('');
  return b.every(function (element) {
    return a.indexOf(element) > -1;
  });
}

mutation(["Mary", "Aarmy"]);

After further research i found that map method returns the argument in string form in mutation function. There fore you have to split both indices into single letters array with split.Example “hello,hey” becomes variable word1 = word[0].split(’""); Here is the new improved code:

function mutation(arr) {
  
let word = arr.map((val)=>{ 
return val.toLowerCase();
});
let word1 = word[0].split('');
let word2 = word[1].split('');
return word2.every(index => word2.includes(index))
}

mutation(["Alien", "line"]);

Thanks hope this helps some one else with similar isue

1 Like

Nice one. I tried this out and the .split() method solve it correctly without issue