Mutations: for loop returns unexpected result [SOLVED]

Howdy all,

In working on the Mutations algorithm exercise, I’ve made a function that mostly works, except for one thing that I didn’t expect and am not sure how to resolve.

My algorithm takes each character of the second string [secondString] in the array, one at a time, and compares it to each character of the first string [firstString]. If there’s a matching character, it gets pushed to a new array I called secondStringCloneArr. When it’s done comparing individual characters, secondStringCloneArr gets joined in a new string, secondStringClone. Then secondString gets compared to secondStringClone. If they match, it returns true.

When I ran a console.log() on the secondStringClone, however, I see that ‘hello’, for example, gets logged as ‘hellllo’. I’m wondering why, when the other letters get pushed to the array in the correct order the correct number of times, the l’s are each being added twice.

Would someone mind having a look at my code and offering suggestions?

Here’s the code:

// Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

// For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.

// The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".

// Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".

function mutation(arr) {
  var firstString = arr[0].toLowerCase();
  var secondString = arr[1].toLowerCase();
  var secondStringCloneArr = [];
  
  for (i = 0; i < secondString.length; i++) {
    for (j = 0; j < firstString.length; j++) {
      var char = secondString[i];
      if (char === firstString[j]) {
        secondStringCloneArr.push(firstString[j]);
      }
    }
  }
  var secondStringClone = secondStringCloneArr.join("");
  // console.log(secondStringClone);
  
  if (secondStringClone === secondString) {
    // console.log("true");
    return true;
  } else {
    // console.log("false");
    return false;
  }
}

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

So I guess I would like the loop to stop iterating through firstString once it finds a matching character. Any suggestions on how to accomplish that? I’ll keep searching and post an update if I find the solution. Meanwhile, suggestions are welcome. Thanks!

Sounds like you want a break statement.

2 Likes

Yes! Thank you @ArielLeslie! That worked perfectly.

Here’s the updated solution:

function mutation(arr) {
var firstString = arr[0].toLowerCase();
var secondString = arr[1].toLowerCase();
var secondStringCloneArr = [];

for (i = 0; i < secondString.length; i++) {
for (j = 0; j < firstString.length; j++) {
var char = secondString[i];
if (char === firstString[j]) {
secondStringCloneArr.push(firstString[j]);
break;
}
}
}
var secondStringClone = secondStringCloneArr.join("");
console.log(secondStringClone);

if (secondStringClone === secondString) {
// console.log(“true”);
return true;
} else {
// console.log(“false”);
return false;
}
}

// mutation([“Hello”, “hey”]);
mutation([“hello”, “Hello”]);