Basic Algorithm Scripting - Mutations

OK, forum folks, I can’t figure out what’s going wrong with my code here. I’m sure it’s something obvious. The only test that is failing is the first one (does “Hello” have all the letters in it that “Hey” does?), which makes me think I have an off-by-one error somewhere, but I can’t see it.

function mutation(arr) {
  //lowercase both strings and separate them out
  let sampleString = arr[0].toLowerCase();
  let testString = arr[1].toLowerCase();

  for (let i=0; i < testString.length; i++) {
    if (!sampleString.includes(testString[i])) {
      return false;
    } else return true;


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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

I have quoted your code and added some line numbers to help us talk about it.
Obviously line 1 is the for loop and loops from 0 to the end of the string. Seems good here.
Let’s step through the code line by line for the case of testString being ‘hey’ and sampleString being ‘hello’

01: i=0, is 0 < 3? yup, continue
02: Is h NOT to be found in the string ‘hello’? Nope, go to the else.

04: return true!!! YAY!

So your code checks if h is not in hello and if the answer is - it is in hello, returns true.
It never loops further.
You can confirm this by adding a console.log in your for loop.

Aha! Thank you so much. So the if/else decision is happening at each step of the for loop, and once it resolves the first time thats it. That explains it.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.