freeCodeCamp Challenge Guide: Mutations

function mutation(arr) {
  word = arr[0].toLowerCase();
  letters = arr[1].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[1].length -1; i++){
if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1)
return false;
}
return true;
}

8 Likes

This is where I was headed. I wasn’t aware I could use arr[1][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.

Hi all,

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

5 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[0] = arr[0].toLowerCase();
  arr[1] = arr[1].toLowerCase();
  var result = [];
  for (i=0; i<arr[1].length; i++) {  
    result[i] = arr[0].indexOf(arr[1][i]);
  } 
  var resultArray = result.filter(function(val) {
    return val===-1;
  });
  if (resultArray[0]===-1) {
    return false;
  } 
  return true; 
}
1 Like

I like this solution it is very similar to what I wanted to write but i do not understand why you put that “arr[1].length -1” why not just “arr[1].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[0].toLowerCase().split('');
    var b = arr[1].toLowerCase().split('');
    while (b.length) {
        if (a.indexOf(b[0]) >= 0) {
            b.splice(0, 1);
        }
        else if (a.indexOf(b[0]) < 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[1].length;i+=1){
for(var j=0;j<x[0].length;j+=1){
if (x[1][i]==x[0][j]){
y.push(1);
break;
}
}
}

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

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

function mutation(arr) {
  return arr[1].toLowerCase().split('').filter(function(letter) {
    return arr[0].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[0] =arr[0].toLowerCase();
arr[1] =arr[1].toLowerCase();

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

     salida=false;
    for (j=0;j<arr[0].length;j++){
      if(arr[0].charAt(j) == arr[1].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[0].toLowerCase().indexOf(arr[1].toLowerCase()[0], 0) !== -1) {
arr[1] = arr[1].slice(1);
return mutation(arr);
} else if (arr[1].length === 0) {
return true;
} else if (arr[0].toLowerCase().indexOf(arr[1].toLowerCase()[0], 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[0].toLowerCase();
      var b = arr[1].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[0].toLowerCase();
  var b = arr[1].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[0].toLowerCase().split("");
 var t = arr[1].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[1].length; i++) {
        if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) === -1) {
            return false;
        }
    }
    return true;
}
5 Likes

That’s my code:

function mutation(arr) {
  
  var wordToCompare = arr[1].toLowerCase();
  var wordToCheck = arr[0].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"]);

good point. thanks. when i do these type of problems i get very deliberate with my javascript…like var a,b,c, but in this case even i would understand what was going on if i cut out the ternary.

return c===b.length;

i will admit when i initially write the code i don’t use ternary syntax i use if/else. then when its time to show someone else i use ternary just because its the standard i guess. probably because i’m still noobish it takes me longer to make sense of ternary.

1 Like

I did this…

function mutation(arr) {
var g = 0;
var ans1 = arr[1].toLowerCase();
arr = arr[0].toLowerCase();
while (g < ans1.length) {
var alet = ans1[g];
var ans2 = arr.indexOf(alet);
if (ans2 == -1) {
return false;
}
g++;

}

return true;
}

not neat and tidy but it worked…