Mutations -- Code Help!

Mutations -- Code Help!
0

#1

Hey, I’m having trouble with this lab and I feel like I have it figured out… But for some reason this code isn’t working as intended. Can someone give me some insight on where I might’ve gone wrong? Or what I’m missing possibly. I’ve done tests and the if statement I feel like should be returning false for this but continues to return true.


function mutation(arr) {
  
var str1 = arr[0].toLowerCase();
var str2 = arr[1].toLowerCase();

var regexp = /[A-Za-z]/gi;

var match1 = str1.match(regexp);
var match2 = str2.match(regexp);

var sort1 = match1.sort();
var sort2 = match2.sort();

  for (i = 0; i < str2.length; i++) {
    if (sort1.indexOf(sort2[i]) === -1) {
      return false;
    } 
    return true;
  }
}

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.


#3

Your for loop is only checking the first letter of str2. It either returns true or false after doing a comparison. As soon as a return statement is reached, a function is done.


#4

Oh man… Okay so I moved my return so its out of the if statement… yet I’m still received wrong true/false answers.


#5

Where did you move your return statement? Moving the return statement out of the if statement is not the issue. You want to return false when the letter is not found. It is the other return statement within the for loop which is causing it to exit out after the first iteration.


#6

Omg lol thanks… Seems like most of my misunderstanding in programming is just the syntax placement. Thanks for clearing another problem up for me.