 # freeCodeCamp Challenge Guide: Mutations

freeCodeCamp Challenge Guide: Mutations
0

# Mutations

## Problem Explanation

• 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…

## Hints

### Hint 1

• If everything is lowercase it will be easier to compare.

### Hint 2

• Our strings might be easier to work with if they were arrays of characters.

### Hint 3

• A loop might help. Use `indexOf()` to check if the letter of the second word is on the first.

## Solutions

Solution 1 (Click to Show/Hide)

Procedural

``````function mutation(arr) {
var test = arr.toLowerCase();
var target = arr.toLowerCase();
for (var i = 0; i < test.length; i++) {
if (target.indexOf(test[i]) < 0) return false;
}
return true;
}
``````

#### Code Explanation

First we make the two strings in the array lowercase. `test` will hold what we are looking for in `target`.
Then we loop through our test characters and if any of them is not found we `return false`.

If they are all found, the loop will finish without returning anything and we get to `return true`.

Solution 2 (Click to Show/Hide)

Declarative

``````function mutation(arr) {
return arr
.toLowerCase()
.split("")
.every(function(letter) {
return arr.toLowerCase().indexOf(letter) != -1;
});
}
``````

#### Code Explanation

Grab the second string, lowercase and turn it into an array; then make sure every one of its letters is a part of the lowercased first string.

`Every` will basically give you letter by letter to compare, which we do by using `indexOf` on the first string. `indexOf` will give you -1 if the current `letter` is missing. We check that not to be the case, for if this happens even once `every` will be false.

Solution 3 (Click to Show/Hide)

Recursive

``````function mutation([ target, test ], i = 0) {
target = target.toLowerCase();
test = test.toLowerCase();
return i >= test.length
? true
: !target.includes(test[i])
? false
: mutation([ target, test ], i + 1);
}
``````
17 Likes

Here is another beginner solution:

``````function mutation(arr) {
var newArr = [];
var temp= 0;
for (var i = 0; i < arr.length; ++i) {
newArr[i] = arr[i].toLowerCase();
}

for (var j = 0; j < newArr.length; ++j) {
if (newArr.indexOf(newArr[j]) < 0)
return false;
}
return true;
}``````
11 Likes

Here’s another solution. In terms of skill I’d say it’s beyond beginner but not quite intermediate.

``````function mutation(arr) {
arr.sort(function(a, b) {
return a.length - b.length;
});

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

return true;
}
``````

This approach first sorts the two strings so that the string with the smallest length is in the first position. Then each character in the string with the smallest length (i.e., index 0) is tested against the characters in the string in the second position (i.e., index 1). If even one character is found not to match, the `mutation` function returns `false`. Otherwise `mutation` returns `true`. There’s also the necessary use of `toLowerCase()` to convert the strings to lowercase.

2 Likes

Here’s my solution. I resorted to adding booleans and checking if the sum matches the length of the second array element.

``````function mutation(arr) {
var word = arr.toLowerCase();
var letterToFind = arr.toLowerCase();
var correctLetters = 0;
for (var i = 0; i < letterToFind.length; i++) {
correctLetters += (word.indexOf(letterToFind[i]) != -1);
}
return (correctLetters === letterToFind.length);

}

mutation(["hello", "hey"]);``````
4 Likes

I like the approach, the only thing I would suggest is to iterate over arr instead of arr since we are only concerned about finding matches for the characters in arr, and instead of checking the indexOf of arr, check it for arr to confirm all the matches. Everything else looks great!

btw what does the first part of your function do with sort? I’m not to great with functions within methods.

in freecodecamp “mutations”, one of the tests looks wrong “mutation([“Alien”, “line”]) should return true.” Can any one help me to know how it is true. Thank you Basic solution:

function mutation(arr) {
var check=0;
for(i=0;i<arr.length;i++){
check=arr.toLowerCase().indexOf(arr[i].toLowerCase());
if(check==-1){
return false;
}

}
return true;
}

2 Likes

another solution :

function mutation(arr) {
var text1= arr.toLowerCase().split("");
var text2= arr.toLowerCase().split("");
var len=arr.length,i,key,flag,j;

``````for(i=0;i<arr.length;i++){
key=text2[i];
{  for(j=0;j<len;j++)
if(key==text1[j])
{   flag=true;
break; }
else {flag=false;
}
}
if(flag===false)
{break;}
}
``````

return flag;
}

mutation([“hello”, “hey”]);

``````function mutation(arr) {
word = arr.toLowerCase();
letters = arr.toLowerCase().split("");
wrong = false;

letters.map(function(letter){
if(word.indexOf(letter) == -1){
wrong = true;
}

});

if (wrong){
return false;
}

return true;
}

mutation(["hello", "neo"]);
``````

Another solution.

My Solution is simply this:

function mutation(arr) {
for(var i = 0; i <= arr.length -1; i++){
if (arr.toLowerCase().indexOf(arr[i].toLowerCase()) == -1)
return false;
}
return true;
}

3 Likes

This is where I was headed. I wasn’t aware I could use arr[i] to look at each individual letter. For some reason I thought that indexOf would search the string for any number of combinations, but I was probably asking a bit much from the method. Thanks for the assist!

1 Like

You are welcome hart143

The world Alien contains the letters: l, i , e, and n. The word Line contains the letters: l, i, e, and n.

Here is my Advanced solution using a Regular Expression:

``````function mutation(arr) {
var re = new RegExp('[^'+arr+']', "i");
return !re.test(arr);
}
``````
27 Likes

@AndrewSwift94 - I think you posted your solution for the wrong challenge.

1 Like

Hi all,

For the basic solution how come we do not need to convert stings to individual characters with.split() before utilizing .indexOf()?

3 Likes

I ended up taking a round about way – initially tried converting the array to separate strings like the Basic Code Solution, but got stuck, so went back to keeping it as an array:

``````  arr = arr.toLowerCase();
arr = arr.toLowerCase();
var result = [];
for (i=0; i<arr.length; i++) {
result[i] = arr.indexOf(arr[i]);
}
var resultArray = result.filter(function(val) {
return val===-1;
});
if (resultArray===-1) {
return false;
}
return true;
}``````

I like this solution it is very similar to what I wanted to write but i do not understand why you put that “arr.length -1” why not just “arr.length”. I though I had understood for loops but these exercises have just shown me that what I learnt earlier does not even scratch the surface. when I see all these solutions, they seem to go againts everything i tought for loops should look like!! for loops are so complex and broad. I really dont like them. It seems like just when you think you get it, there is another BUT…

1 Like