Bonfire: Mutations – Best Solution

September 25, 2015

As always, there are a multitude of ways to solve any coding challenge. In this particular case, I used a loop that continued to loop through as long as the result kept returning true. Why did I solve it this way? Well, why continue running a function when you’ve already got your answer before it completes all of its iterations. As soon as we get the first failure, we can stop the function and return false.

The goal of this challenge is to: Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

Best Solution

```function mutation(arr) {
var first = arr[0].toLowerCase();
var second = arr[1].toLowerCase();

for(i = 0, len = second.length; i < len; i++){
result = first.indexOf(second[i]);
if (result === -1) {
return false;
break;
} else {
continue;
}
}
return true;
}

mutation(['hello', 'hey']);```

CodePen Solution

See the Pen Bonfire: Mutations – Best Solution by Mike Doubintchik (@allurewebsolutions) on CodePen.

Code Breakdown

From Mozilla:

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

Free Code Camp gives Array.indexOf() as the suggested method for this challenge. I decided to go with String.indexOf() as it saves us the step of converting our strings to arrays and it works exactly the same.

We start by pulling out the first two elements into their own variables first and second, while at the same time converting them to lower case, which makes future comparison easier.

Then we write a loop that goes through the second element/variable to check if each letter exists in the first element/variable. This can be a “for” or “while” loop. I just decided to go with a for loop for it’s shorter notation.

Inside the loop, we first check whether the return of the indexOf() method returns a “-1.” If it does, that means that that particular letter does not exist in the first variable. Otherwise, it would give us the position of that element in the first variable, either “0” or higher. A quick note, thanks to Aman from the comments, I’ve added code to save the length of the second array to a variable so it doesn’t have to be calculated on each loop iteration.

The reason we want to check for a failure first is because we can stop the loop and give the needed output of return false right away. No need to keep running code.

Assuming the results is continuously true, then our “for” loop will continue looping through each letter until it finally spits out the result of return true.

Alternative Solution

```function mutation(arr) {

var first = arr[0].toLowerCase();
var second = arr[1].toLowerCase();

for(i = 0; i < second.length; i++){
result = first.indexOf(second[i]);
if (result === -1){
return false;
}
}
return true;
}

mutation(['hello', 'hey']);```

This solution is shorter slightly. Which I’ve mentioned before is good. We always want to minimize the amount of code we write. Except when shorter code is less efficient when it’s actually running. For such a small function as this, it really doesn’t matter to have more efficiently running code. However, when functions get more and more complicated you want to optimize them.

In this solution, we loop through the whole second variable. At the end, if any of the results equaled “-1” we return false, otherwise we return true.

Alternative Solution 2 (Provide by Diaa Abidou)

```function mutation(arr) {

var arr2 = arr[1].toLowerCase().split('');

for (var i = 0, len = arr2.length; i < len; i++) {
if (!arr[0].includes(arr2[i])) {
return false;
}
};

return true;
}```

Discussion

I would love to hear others’ solutions and discover better and cooler ways to solve these challenges. Please comment with your questions, suggestions, or anything you would like.

If you found my solution useful or learned something new from this blog post, please feel free add kudos inside the main chat of Free Code Camp: Thanks @allurewebsolutions

Author allure

• Aman Agarwal says:

A small tip here for optimizing loops – instead of using the .length() function in the second condition of for loops, such as “i < array.length", try to declare the length beforehand in the first condition: such as, "i=0, len=array.length; i<len; i++"

Unless the array length is going to be changed within the loop, It is better to compare i to a fixed number otherwise it will run an extra function (and maybe even iterate over the whole array) in every iteration of the loop. The larger the array is, the slower it makes the program. 🙂

• Good idea! I didn’t think of that. I’ve updated the code to include your suggestion.

I need to start running benchmarks on these things.

• Diaa Abidou says:

Thanks for your answers, here is my code in case you find it useful and compact:

```function mutation(arr) {

var toCompareWith = arr[1].split("");

for (var i=0 ; i<toCompareWith.length ; i++){
if (!arr[0].toLowerCase().includes(toCompareWith[i].toLowerCase())){
return false;
}
}

return true;
}

mutation(["hello", "hey"]);
```
• Diaa, thank you for your solution. It looks great. Very short and clean. I like your use of the includes() function.

I’ve modified your solution trivially and added it as an alternative solution to the blog post 🙂

Designed by