Basic Algorithm Scripting: Mutations

Basic Algorithm Scripting: Mutations
0

#1

Can someone please tell me why my code (below) isn’t working for this problem?

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

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

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to 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.

  1. You are checking for Str2.indexof(i), but Str2 most likely doesn’t contain i (and if it does, it will just return the index). Instead you just want to check against the i-th character in the string: Str2[i].
  2. You have to loop through Str2 before you can conclude if you should return true. Your code will return true if the first character in Str2 is found somewhere in Str1. You will have to reverse the statement and return false if the character is not found.