Mutations Scripting challenge

Having some trouble with this basic scripting problem. First one I haven’t been able to find that didn’t have much help already to be found on the forums. Banged my head against it for a few hours now and feel like both my skull and the problem are just about to get cracked. It works in most cases I plug in but I think that just might be the way the nested if function in structured. Also do I need to even both including the first sort that puts the larger of the two strings first? None of the test cases call for it but I feel like it could cause issues if the second argument was longer than the first…

function mutation(arr) {
  var words=arr.join().toLowerCase().split(",").sort(function(a,b){return b.length-a.length;});
  var wordOne=words[0].split("").sort();
  var wordTwo=words[1].split("").sort();
  for(var i=0;i<wordTwo.length;i++){
    if (wordOne.indexOf(wordTwo[i])!=-1){return true;}else{return false;


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

Your code is doing a lot of work that doesn’t need to be done, but there are only a couple of problems preventing you from passing:

  • When you sort the array, you are sometimes switching the order of the words. The input is dependent on the order of the words, so you’re not getting the comparison that you are expecting.

  • Your if clause always returns something. Try placing a console.log(i) in your for loop. What do notice about this?

You can do this, so I’m being vague. If you need to, sleep on it, then do some rubber duck debugging.

So I came here from Codeacedemy which had console log built into the interface and had no idea how to pull it up through the web developer tools in my browser until like just this minute. I’ve been going through like half these lessons blind so now that I can actually see what’s happening debugging is so much less frustrating. Haven’t solved the problem yet, but now I can at least see my work as I’m doing it. I see that I don’t get past the first index so now I have some clue of what I need to do. Thanks! I’ll post the finished code when I bet this guy.

They aren’t in the code I posted here b/c I didn’t want it to look cluttered, but I usually comment about the functionality of each line as I go just to keep process straight.

1 Like

I am interested to know how you used the Console with this. I pull up developer tools on Free Code Camp and I am unable to figure out how to use both in tandem.

You can use the console built into your browser: if you use chrome, just press ctrl-shift-I. I will often wrap the test call they build into the challenge in a console.log() in addition to any that I need in the midst of my code.

1 Like

That’s the first time I’ve heard the term “rubber duck debugging.” I like it. That’s actually part of why I like teaching people: cause I learn it better myself.


Yeah they is actually a FCC thing about it later on that I found in the Map with a quick search

You can toggle it so that it split screens or wraps with the browser and it makes things a good bit easier to figure out what’s what.

Yeah I find breaking it down as simply as possible is very helpful to figure out first principles. Also, I just want to thank you for taking the time to try and parse the ideas here out for people who might not be as far down the rabbit hole. Every time I see you name in the forums I know I’m going to walk a way with a better understanding of the material. You are what open source is all about, sir, and I’m glad to hear that talking it out with people is helping you get something out of it as well.

So I was doing everything backward lol:

function mutation(arr) {
  //define term to be searched against
var wordOne=arr[0].toLowerCase();
//define term which you use to search 
  var wordTwo=arr[1].toLowerCase().split("");
  //iterate through the length of wordTwo
  for(var i=0;i<wordTwo.length;i++){
    //code searched through wordOne based on the index of word two and if it is less that zero (ie, false), then it will evaluate is false. Basically it disqualifies any words that don't fit the criteria
    if(wordOne.indexOf(wordTwo[i])==-1){return false;}
  //if the thing passes the criteria of the for loop that disqualify it, then if must be true and so returns that way.
  return true;
mutation(["hello", "hey"]);

So I’m not totally sure why I can’t put the return true as an else statement in the for loop. I put it on the outside to see what would happen and it worked but I’m not sure about the principle behind it. I know returns dump you out of the loop, but in case:hello/hey, every time I thought I had it pinned down it in the console would jump to the other part of the conditional statement when I ran it.

What you were telling the loop is “If this matches, return true. If it doesn’t, return false”. When you use else, you’re not giving the code any option other than to return something.

Also of note, you don’t need to split the string at all. You can iterate through a string just as though it were an array.


Alright, cool. I guess I was figuring that in order for things to work right it had to be contained in one for statement but what I’m doing now proves if something isn’t matching and then whatever is left over must be true.

I will say that the split made me visualize what was happening better so for purposes of solving it helped but I see why I didn’t need it now that I’m on the other side lol. Thanks again for the clarification, sir.