Mutations: Why doesn't this work?

Tell us what’s happening:

Hi! Not sure why this isn’t working. This is how I think my solution works:

  1. The first loop looks at the first letter of the second string ("h"ey)
  2. I created a variable called individualArray to hold the letter ("h"ey)
  3. The second loop loops through the first string (“hello”)
  4. If individualArray doesn’t match a letter in the first string, it returns false. Otherwise it goes back to step 1 and goes to the second letter (h"e"y).
  5. If there is no “false”, the default return is “true”

Is this not a sound approach? Is there another approach I should be taking?

Your code so far


function mutation(arr) {
for (var i = 0; i < arr[1].length; i++){
  let individualArray = arr[1][i];
    for (var j = 0; j < arr[0].length; j++){
      if (individualArray !== arr[0][j]){
      return false
      } 
    }
}
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/80.0.3987.116 Safari/537.36.

Challenge: Mutations

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations

Why is there only one closing symbol the ;
Also it should return true but, it doesn’t.

you will always get false, what is this loop doing?
it is checking one single letter of arr[1] against all the letters of arr[0]

you will need to check in a different way
you can try with some methods (indexOf, includes are usually life savers when confronting arrays or strings - search the documentation on them!) or a different approach with your loops

1 Like

Thanks for the feedback. I was able to solve the problem with the below. One thing I wanted to get your opinion on…

Sometimes I feel like my solutions are too drawn-out. Even if I get it right, it seems that my solutions are anything but the “clean” code that I see other developers espouse. This is especially the case when I review other solutions and they are more succinct. Is this something I should worry about now?

I’m assuming with time and experience, you learn ways to make your code shorter and more concise. I guess I’m worried about developing bad habits early on that will be hard to shake later…

function mutation(arr) {
var firstArr = arr[0].toLowerCase();
var secondArr = arr[1].toLowerCase();

var holder = [];
for (var i = 0; i < secondArr.length; i++){
  let individualArray = secondArr[i];
  holder.push(firstArr.indexOf(individualArray));
}
if (holder.includes(-1)) {
 return false
} else {
 return true
}
}

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

I’m still afraid that I’m not following best practices all of the time. I know that my code works and that’s what makes people happy. I’ve found that as time goes on, I never really build bad habits, but I learn new (to me) implementations.

As long as you surround yourself in an environment where you get to experience others code, you should be constantly learning. I had been using nested for loops because they work until my friend recommended I try key lookup instead. I found key lookup was faster than nested for loops and implemented that instead.

If you’re worried about the bad habits still, a co-worker of mine told me about patterns. Apparently they’re like “best practices” but I can’t find any definitive answer on what the best practice is for everything. I would give

Javascript patterns

A Google and see what you can find. Hopefully that’ll shed some light onto your question.

1 Like