Basic Algorithm Scripting: Mutations - where to start

The curly brackets have kind of gotten all over the place, let me give you the shape of what it should look like.

for (/* each character in word2 */) {
  if (/* character not found */) {
    return false;
  }
}

// after the loop (all characters found)
return true;

I’m not sure I quite understand.

Are you saying the code I had before was way off the mark?

Are the slashes indicating I should use regex?

I see only return false - when would we return true then?

-increasingly confused

sorry - I see return true after the loop.

@colinthornton, are you able to explain how

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
works?

After reading the MDN explanation several times over the last few hours, I am unable to grasp how it applies to this exercise (I thought I did but whatever I do doesn’t seem to work so far).

The challenge guide

Suggests using

String.indexOf()

But I don’t understand how this applies either.

They are just comments. You need to fill those comments in with code that applies the logic I suggested.

You need to loop over each character in word2.
For each character in word2, determine if word1 contains that character.
If it does, then do nothing because we need to check the rest of the characters.
If it does not, then return false.
If we complete the loop without ever having returned false, then we know that word1 contains every character in word2, and can therefore return true.


Loop over each character in a word

We can loop over each character in the word with a for loop.

const word = 'word';
for (let i = 0; i < word.length; i++) {
  const character = word[i];
  // do something with `character`
}

Does a word contain a character?

We can find if a word contains a character or not with the String.includes method. It returns a boolean.

const word = 'word';
word.includes('w'); // true
word.includes('a'); // false
2 Likes

Thanks for that. Spent another hour trying different things with return statements and checking variable values with console.log. Still doesn’t work. I understand the logic of what I am supposed to do but I don’t understand what code will make that happen. Here’s my latest code.

function mutation(arr) {
  let word1 = arr[0].toLowerCase();
  let word2 = arr[1].toLowerCase();
  word2 = word2.split('');
  //console.log(word1);
  //console.log(word2);
  for (let i = 0; i < word2.length;  i++) {
      let character = word2[i];
      if (word1.includes(character)) {
        //console.log(word1);
        //console.log(word2[i]);
      }
      }
      return false;
  } 

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

@camperextraordinaire and @colinthornton,
I appreciate your help but I am becoming increasingly confused as I am not clear if each one of you is taking the same approach and advising me on different aspects of that approach; or if each one of you is taking a slightly different approach; or if each one of you is taking a completely different approach.

In other words, should I pick one approach or the other, or combine all of your suggestions together, or pick and choose which suggestions might apply? The latter approach seems to be too much for me to accommodate intellectually at this point.

In any case I am not sure how to proceed.

@camperextraordinaire thanks for the review. I took out some curly braces and changed the return statements. Not completely understanding why this code works.

Also you mentioned I am missing an else statement.

However the below code passes without an else statement. How is this possible? More confused than ever.

function mutation(arr) {
  let word1 = arr[0].toLowerCase();
  let word2 = arr[1].toLowerCase();
  word2 = word2.split('');
  
  for (let i = 0; i < word2.length; i++) {
    if (word1.includes(word2[i]) != true)
      return false;
    } 
      return true;
}

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

Sorry, I am not following all the console.log statements. In both your examples I see else if and else statements but I don’t have any of them in my code. Do I need them?

Which closing curly bracket? There are two.

Where would we add the curly brackets? You mentioned earlier

So was thinking that previously I had too many curly brackets?

Also, wouldn’t the program stop running after we return false, and therefore never return true? This was the issue I was having previously with the return statements.

You mentioned previously that I was

Where is it missing and how does it work without the else statement?

Increasingly confused even after my code passed! Not sure what I’ve learned here after several hours of working on this.

I’m sorry, but FYI, this is too much information for someone who is already confused. I’m having enough difficulty just focusing on the current challenge.

But I persist in following up on your prior statement

Was this incorrect? If so, that is fine, I just want to be clear on this.

Again -

Is the above incorrect?

To be clear -

Are you saying

“It is correct” that “return will stop the function at the first coincidence”

or

“it is correct” that the “above is incorrect” (meaning that return will not stop the function at the first coincidence

?

Any response on this please? I just want to be clear.

Well things are still a bit vague but I’m just going to move on for now.

@camperextraordinaire thanks for all your help! I appreciate it.

You should make sure you understand what part you were confused with, or had a hole in your knowledge about, that held you back from solving the algorithm on your own. You stated the algorithm in plain English at some point, so you understood what needed to be done, but were unable to convert that logic into JavaScript.

From the above it sounds like you were confused about if and else statements. What parts were you hung up on, and what have you learned to the contrary by doing this exercise?

1 Like

If you have more questions or want clarifications about this problem or anything else with regards to web development, then I’m happy to keep talking over direct messages if you prefer.

@colinthornton, to be honest, I’m not clear on what parts I was hung up on, or what I’ve learned about JavaScript doing this exercise. That’s part of my confusion - after apparently “solving” this challenge I’m not clear on what I know/understand and what I don’t know/understand.

I wouldn’t say that I was confused in general about if/else statements, only that @camperextraordinaire had said at some point that I was missing an else statement yet my passing code did not have an else statement so I was not clear on what he meant.

I will say that I learned:

  1. The exercise instructions were very vague and held me back from having a good enough understanding of what I needed to do to solve the algorithm on my own. I have found this to be the case with several of the FCC exercises I’ve gotten seriously stuck on. It would have been helpful to have an example or more elaboration on what methods might be used. I’ve learned that I probably did not need to spend several hours on this exercise, if only the instructions were more clear.

  2. It is difficult to have more than one person responding to my questions. In general I found the responses to be a bit vague. More importantly, the various responses seemed contradictory and it seemed like each person was taking his own approach, leading to more confusion on my part. See my prior comment above.

In any case, thanks for your willingness to help, I do appreciate your time and effort!

@camperextraordinaire, thanks for the suggestion. I’ll take a look at the various solutions to see how they relate to mine. Thanks for all your help, I do appreciate it!

After struggling quite a bit, here is my code: (hope it helps someone :))

function mutation(arr) {

  let result = true;
  let str1 = arr[0].toLowerCase();
  let str2 = arr[1].toLowerCase();
  
  let splitArr1 = str1.split("");
  let splitArr2 = str2.split("");

  console.log(splitArr1 , splitArr2);

      for (let i = 0; i < splitArr2.length; i++)
      {

          if(splitArr1.indexOf(splitArr2[i]) === -1)
          {
            result = false;
          }

      }  
  return result;  
  }
  mutation(["floor", "for"])