I wanted to try what i understand first

Tell us what’s happening:
Can someone help me solve this code without changing the logic?
so i can learn and understand where my mistakes was

thank you

Your code so far


function mutation(arr) {
var check = 0;
var word1 = arr[0].toLowerCase().split("");
var word2 = arr[1].toLowerCase().split("");
for (var i=0; i<word1.length; i++) {
  for (var j=0; j<word2.length; j++) {
    if (word1[i] == word2[j]) {
      check +=1;
      if (check == word2.length){
        return true;
      }
    }
  }
}
return false;
}

mutation(["voodoo", "no"]);

Your browser information:

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

Challenge: Mutations

Link to the challenge:

Probably not because the logic is wrong :slight_smile:

Think carefully about what the challenge is asking you to do. You get two strings passed in. You want to make sure that all of the letters in the second string are in the first string. So which string do you need to go through all of the letters one at a time and make sure they are in the other string? That will be the string you want to iterate through (with your for loop).

1 Like

Hi, it is great to use console.log(check) after your first loop for aware the problem.
You will see your check stops counting after it match.

if (check == word2.length){
return true;

whenever it matchs with array.length, it immediately breaks the loop and your function returns true.

it is really good practice using console.log() for debuging or finding mistakes.

Take a look on your console with this

function mutation(arr) {
var check = 0;
var word1 = arr[0].toLowerCase().split("");
var word2 = arr[1].toLowerCase().split("");
for (var i=0; i<word1.length; i++) {
  for (var j=0; j<word2.length; j++) {
    if (word1[i] == word2[j]) {
      check +=1;
      if (check == word2.length){
        return true;
      }
    }
  }console.log(check)
}
return false;
}

mutation(["voodoo", "no"]);
1 Like

Thank you alright let me work on solving it with different approach.

Okay that is one of the greatest challenges am facing with javascript i dont see my output … do you know anyway i can check as i code i mean run the program like other programming like C for example you can use codeblocks or python use can use Idle. for javascript i have tried installing node … but face some problems any other ways apart from browser console? thank you in advance i know i can google it just wanted your opinion.

This works if i had known the includes() method exist would have been alot easier :smiley:

function mutation(arr) {

  var check = 0;

  var word1 = arr[0].toLowerCase()

  var word2 = arr[1].toLowerCase()

  for (var i=0; i<word2.length; i++) {

      if (word1.includes(word2[i])) {

        check +=1;

        if (check == word2.length){

          return true;

        }

      }

  }

  return false;

}

mutation(["voodoo", "no"]);
1 Like

Congrats on passing the test! And you learned a new Array method as well :slight_smile:

Now take a good close look at what you have there and see if you can make it even simpler. I’ll give you a hint. You don’t need the check counter variable at all. You are doing the for loop correctly, so you don’t need to change that. It’s what’s inside the for loop that can be simplified.

1 Like

Hi @swisscodes !

I added spoiler tags around your code for those who have not done this challenge.

I’ve also edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Okay Great job thank you… ill look into adding the spoiler next time.

Plus also using the “preformatted text”

This time if you can please help with the simplifying code i will be happy to learn.

Because if believe i have to use the check variable so i don’t return true before completion of checking every character in word2…

Thank you

Well, I don’t want to just give you the answer. Remember, there are two values you can return: true or false. You are checking that each letter in word2 is also in word1 using the for loop. Right now you are incrementing the check counter if it is. But what if it isn’t in word1?