# freeCodeCamp Challenge Guide: Mutations

``````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.

2 Likes

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;
}

7 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!

2 Likes

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);
}
``````
58 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()?

4 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
``````function mutation(arr) {
var a = arr.toLowerCase().split('');
var b = arr.toLowerCase().split('');
while (b.length) {
if (a.indexOf(b) >= 0) {
b.splice(0, 1);
}
else if (a.indexOf(b) < 0) {
return false;
}
}
return true;
}``````
1 Like

Beginner long solution:
function mutation(arr) {
var x = arr;
var y = [];
for (k=0;k<arr.length;k+=1){
x[k]=x[k].toLowerCase();
}

for(var i=0;i<x.length;i+=1){
for(var j=0;j<x.length;j+=1){
if (x[i]==x[j]){
y.push(1);
break;
}
}
}

for (var l=0;l<y.length;l+=1){
if(y.length == x.length){
return true;
}
}
return false;
}

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

``````function mutation(arr) {
return arr.toLowerCase().split('').filter(function(letter) {
return arr.toLowerCase().split('').indexOf(letter) < 0;
}).length > 0 ? false:true;
}``````
1 Like

So basically, I made a solution without the method indexOf(). i’m still trying to explain to myself why I did that, I think old habits die hard.

function mutation(arr) {

var salida;
arr =arr.toLowerCase();
arr =arr.toLowerCase();

for(var i=0;i<arr.length;i++)
{

``````     salida=false;
for (j=0;j<arr.length;j++){
if(arr.charAt(j) == arr.charAt(i)){
salida = true;

}

}
if (!salida){return false;}
}
``````

return salida ;
}

mutation([“helLo”, “nao”]);

I don’t see a recursive solution, so here’s my take:

function mutation(arr) {

//recursive.

if (arr.toLowerCase().indexOf(arr.toLowerCase(), 0) !== -1) {
arr = arr.slice(1);
return mutation(arr);
} else if (arr.length === 0) {
return true;
} else if (arr.toLowerCase().indexOf(arr.toLowerCase(), 0) === -1) {
return false;
}
}
mutation([“Alien”, “oiNe”]);

### This is a whacky thread. Here is yet another working solution. It might be more basic than the basic solution. It just counts the matches and returns true if the number of matches equals the length of the test string. Surprised I haven't seen anyone else use .includes(). It is the most literal method...indexOf being -1 never even occurred to me. I think I should get bonus nerd points for including c++ in my function....

``````  function mutation(arr) {
var a = arr.toLowerCase();
var b = arr.toLowerCase().split('');
var c = 0;
for(var i = 0; i <= b.length; i++){
if(a.includes(b[i])){
c++;
}
}
return c === b.length ? true : false;
}
``````

### On a weird note the following works with pretty much anything in place of freeCodeCampRules....ternary syntax is weird

``````function mutation(arr) {
var a = arr.toLowerCase();
var b = arr.toLowerCase().split('');
var c = 0;
for(var i = 0; i<=b.length; i++){
freeCodeCampRules = a.includes(b[i]) ? c++ : null;
}
return c===b.length ? true : false;

}``````
4 Likes
``````function mutation(arr){
var d = arr.toLowerCase().split("");
var t = arr.toLowerCase().split("");
var w = [];
for (var j = 0; j < t.length; j++) {
w.push(d.indexOf(t[j]));
}
return ee = w.every(function(val){
return val !== -1
});
}
mutation(["hello", "hey"]);
``````

I like your code, but I did notice one thing.
Your use of the ternary operator in your return expressions is redundant.

`return c === b.length;`

is already a comparison operation that returns a boolean value. There’s no need to define it with an additional ternary operator.

1 Like

Here’s what I came up with:

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

That’s my code:

``````function mutation(arr) {

var wordToCompare = arr.toLowerCase();
var wordToCheck = arr.toLowerCase();
var countMatch = 0;

for(var i=0; i<wordToCompare.length; i++){
//ceck if the letter from second string match any letter from the first string
if(wordToCheck.indexOf(wordToCompare[i]) !== -1){
countMatch++;//count matches
}
}
//check if all the letter from the wordToCompare matched
if(countMatch === wordToCompare.length){
return true;
}
return false;
}
//test
mutation(["hello", "hey"]);``````