Please help with this algorithm challenge

I can’t reply to the hint post so I think I will ask here.
My code works for most of the tests in this challenge.
Try pasting my code and look through the tests and it’s really weird.
It passed :
mutation([“Mary”, “Aarmy”])
mutation([“hello”, “hey”])
But it won’t pass :
mutation([“Alien”, “line”])

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

mutation(["hello", "neo"])

You know what’s funny?
Change the “neo” in the last line to “leo” and you would get
TypeError: Cannot read property ‘toLowerCase’ of undefined

What?.. I’m banging my head over the wall for more than 2 hours now. Don’t know why it doesn’t work.

Link to the challenge:

Hi @ndv !

Welcome to the forum!

I have moved your post to it’s own topic.
Whenever you have questions about a challenge it is always best to create a new post and people can assist you there.

I was able to look at your code.

I think it would be a lot easier if you just use one for loop and the if statement.
The nested loop makes things a lot more complicated.

Then your code will look like this

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

mutation(["hello", "neo"])

In your if statement, you just have to add on small thing here
arr[1].toLowerCase() that checks if that letter is found in the first word.

Also, you don’t need to add split here

You could just write

Hope that helps!

Hi @jwilkins.oboe !
Thanks for the reply.
I actually can get it to work after looking at the solution.
Javascript was so weird. Sometimes string can act like array.
Anyway, I’m really curious about the “TypeError: Cannot read property ‘toLowerCase’ of undefined” part.
Could you please explain that too?

Hmm… not entirely sure.

All I know was that the error was coming from here

If you delete the [j] then the error goes away.

Maybe someone else has the answer

1 Like

A few things here.

function mutation(arr) {
  // This condition will convert your second string to an array EVERY LOOP ITERATION
  // Why not just convert to a lowercase array once?
  for (let i = 0; i < arr[1].toLowerCase().split("").length; i++) {
    // This condition will convert your first string to an array EVERY LOOP ITERATION
    // Why not just convert to a lowercase array once?
    for (let j = 0; j < arr[0].toLowerCase().split("").length; j++) {
      // j is over the length of arr[0].... so you shouldn't index into arr[1] based on j
      // Also, you shouldn't keep repeating the conversion to lower case
      if (arr[0].toLowerCase().indexOf(arr[1][j].toLowerCase()) == -1)
        return false;
    }
  }
  return true;
}

mutation(["hello", "neo"])
4 Likes

My answer for this challange look like almost exactly like 1st solution given by FCC, depend on the problem i think before jump to main code, sometimes better to make abstraction first, made your code more clear and easier to read.

When I was doing mine, I converted the two input words to arrays, then defined the empty “target array”. It worked, overall… Want to make it a little bit more compact, at some point…

for (let i = 0; i < sndWrdLowC.length; i++) {
      if (fstWrdLowC.includes(sndWrdLowC[i])) {
        trueArr.push(sndWrdLowC[i]);
        
      } else {
        if (sndWrdLowC !== trueArr)
     return false; 
     
      }

Please look at your code again and ask yourself - what are you doing and what is the actual goal?
Here is a hint: You only need one for-loop and no nesting :wink:

I added spoiler tags to your working solution.

Personally, I’m not a fan of funky, hard to read abbreviations as part of making code ‘compact’. Compact isn’t really automatically better.

1 Like

Yes, completely agree. May not need to make a code shorter, and looking for ways to “standardize” my variable names. Thanks!

Guys, I do appreciate all the answers.
But the real question was what is the cause of that TypeError at the end.

Did you read my comments in your code? The comment about j vs i is exactly the cause of the type error. Bad indexing leads to undefined values, which leads to that specific type error.

The error tells you “what” it is: You create an None-object in your code and try to call a method “.toLowerCase()” on it, which it doesn’t have.

Yes, I did read it.
The “neo” and “leo” is same in length that’s why I asked.
With the “neo” you would not get the Type error.
Sorry, may be I was just too much skeptical.

With "neo", the very first character is not matching, so the indexing problem is not apparent. All of the characters in "leo" match but "hello" is longer, which is why you get the indexing problem. Your indexing must be using the actual length of the string you are indexing into.

1 Like

Thanks! I get it now.

1 Like

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