Mutation challenge

Mutation challenge
0

#1

To the group:
I am stuck on the following challenge:

[Mutation](https://www.freecodecamp.com/challenges/mutations#?solution= function%20mutation(arr)%20{ %20%20 %20%20var%20falseStorageContainer%20%3D%20[]%3B %20%20%20var%20truthStorageContainer%20%3D%20[]%3B %20%20 %20%20var%20toLowerCaseConvert%20%3D%20arr.join("%20").toLowerCase()%3B %20%20var%20converToArray%20%3D%20toLowerCaseConvert.split("%20")%3B %20%20 %20%20 %20%20var%20sliceArray%20%3D%20%20arr.slice(0%2C1).join("").toLowerCase()%3B %20%20var%20turnInToArray%20%3D%20sliceArray.split("")%3B %20%20 %20%20var%20sliceSecondArray%20%3D%20converToArray.slice(-1).join("")%3B %20%20var%20turnInToSecondArray%20%3D%20sliceSecondArray.split("")%3B %20 %20%20 %20%20 %20%20 %20%20for%20(var%20i%20%3D%200%3B%20i%20<%20turnInToArray.length%3B%20i%2B%2B%20){ %20%20%20%20 %20%20if%20(turnInToArray.indexOf(turnInToSecondArray[i])%20<%200)%7B%0A%20%20%20%20var%20falseContainer%20%3D%20falseStorageContainer.push(turnInToSecondArray%5Bi%5D)%3B%20%0A%20%20%20%20return%20false%3B%0A%20%20%20%20%0A%20%20%20%20%7D%20return%20true%20%3B%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%7D%0A%20%0A%20%20%20%20%20%0A%20%20%0A%20%20%0A%7D%0A%0Amutation(%5B%22zyxwvutsrqponmlkjihgfedcba%22%2C%20%22qrstu%22%5D)%3B%0A)

Could someone help me understanding what I am missing within my code where I can’t pass this challenge. I can pass all of the challenges except:

mutation([“hello”, “hey”]) should return false

For the reason the machine stops iterating through “hey” once it reaches “h” i.e., it stops iterating early.

See below for my code:

function mutation(arr) {
  
  var falseStorageContainer = [];
   var truthStorageContainer = [];
  
  var toLowerCaseConvert = arr.join(" ").toLowerCase();
  var converToArray = toLowerCaseConvert.split(" ");
  
  
  var sliceArray =  arr.slice(0,1).join("").toLowerCase();
  var turnInToArray = sliceArray.split("");
  
  var sliceSecondArray = converToArray.slice(-1).join("");
  var turnInToSecondArray = sliceSecondArray.split("");
 
    
  for (var i = 0; i < turnInToArray.length; i++ ){
    
  if (turnInToArray.indexOf(turnInToSecondArray[i]) < 0){
    var falseContainer = falseStorageContainer.push(turnInToSecondArray[i]); 
    return false;
    
    } return true ;
    
   }
 
 }

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);

#2

First thing’s first:

Format links like this: [link text](URL)
Format code like this:

```
code
```

As for your algorithm, there’s far too much going on there. You don’t need to turn the elements of the array into their own arrays, because the only way you need to modify them is by lowercasing them, which can be done directly on strings. No need for split() or join().

Try first writing it out in pseudocode based on the instructions - that should help you wrap your head around the logic before writing out the JavaScript.


#3

I took a look at your solution. I think you probably need to start from scratch. However, to answer your question, the reason that it stops early and only iterates once is because you are returning either false or true in the for loop, no matter what; The return statement will return execution back to calling function, thus exiting your for-loop and the mutation method altogether. To fix that you would move the return true statement out of the for-loop.

There’s a lot of other things I wanted to point out about your code though:

  • You’re converting the entire array into one string to convert it to lower case. This is unnecessary.
  • arr.slice(0,1).join("").toLowerCase(); - You’re accessing the first array element and then converting the entire array to a string. This is unnecessary.
  • You need to keep in mind that the array is an array of strings, and an array element can be accessed by simply arr[0] or arr[1] for the 2 different elements.
  • You don’t need either of the container arrays.

This entire solution could be done in 2-3 lines if you eschew the use of variables.


#4

I fixed the link to your solution.
I also cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.