freeCodeCamp Challenge Guide: Mutations

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…

That sort function is interesting, I’ll have to research that! I have a question about your for statement, you’re declaring a global variable len and using it to test the length of arr, but you could just use i for that, the extra variable isn’t necessary? Anyway, genuinely curious!

Hello there, I found this solution: there is no need to sort the array or split it :slight_smile:

function mutation(arr) {
  i=0;
  while (i<arr[1].length) {
   if (arr[0].toLowerCase().indexOf(arr[1].charAt(i).toLowerCase())>=0){
       i++;
       } else {
      return false;
    }    
  } 
  return true;
} 

Let me know what you think!

Hi guys i recently finished this challenge and came up with this solution!

function mutation(arr) {
  string = arr[0].toLowerCase();
  substring = arr[1].toLowerCase().split("");
  
  for(i = 0; i <= substring.length; i++) {
    if(string.indexOf(substring[substring.length - 1]) !== -1) {   
      return string.indexOf(substring[i]) !== -1;
    }
    return false;
  }
  
}
mutation(["Hello", "HELL"]);

I know it works, but im aware i have some bad practices, can you guys let me know.

I guess I made it the worst possible way, but it works as well :disappointed:

function mutation(arr) {
result = 0;
arr3 = [];
arr[0] = arr[0].toLowerCase();
arr[1] = arr[1].toLowerCase();
str1 = “”;
str2 = “”;
for (i=0; i<arr[0].length; i++) {
if(str1.indexOf(arr[0][i])==-1) {
str1 += arr[0][i];
}
}
for (i=0; i<arr[1].length; i++) {
if(str2.indexOf(arr[1][i])==-1) {
str2 += arr[1][i];
}
}
if (str1.indexOf(str2) >= 0) {
return true;
} else {
for (i=0; i<str1.length; i++){
for (j=0; j<str2.length; j++){
if (str1[i].indexOf(str2[j]) === 0) {
result += 1;
}
}
}

  if (result===str1.length) {
    return true;
  } else if (result>str2.length){
    result = str2.length;
    return true;
  } else if (result === str2.length){
    return true;
  } return false; 
}

}
Hope I’ll get smarter than this :slight_smile:

Here is my beginner solution:

function mutation(arr) {
  var first = arr[0].toLowerCase().split('');
  var second = arr[1].toLowerCase().split('');
  var cont = 0;
  
  for(i=0;i<second.length;i++){
    if(first.indexOf(second[i]) !== -1){
      cont++;
    }
  }
  
  if(cont === second.length){
    return true;
  }else{
    return false;
  }
      
}
2 Likes

Why this code: (see the comment)

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

with this one: (see the comment)

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

have different result?

1 Like

You can also use a set. It is more efficient for lookup.

function mutation(arr) {
  var set = new Set(arr[0].toLowerCase().split(''));
  for (var i = 0; i < arr[1].length; i++)
    if (!set.has(arr[1][i].toLowerCase()))
      return false;
  return true;
}

Basically I’ve created a set containing all the characters of the first string and then checking if any character of second string is missing.

1 Like

In your second example, the one with a curly brace, you’ve forgotten to include a closing curly brace for the if statement.

This is my proposed solution.

function mutation(arr) {
  var el1 = arr[0].toLowerCase().split("");
  var el2 = arr[1].toLowerCase().split("");
  for(var i = 0 ; i < el2.length; i++) {
    if(!el1.includes(el2[i])) {
      return false;
    } 
  }
  return true;
}

Is the .indexOf method faster than .includes?

1 Like

My solution. I don’t know why, but I could not use “indexOf ()” properly. I then looked in the documentation for “indexOf ()” and found “inculdes”. With this function, I quickly found a solution. Happy to have discovered “every ()”.

function mutation(arr) {
arr = arr.join(" “).toLowerCase().split(” ");

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

return true;
}

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

Edit : Shit ! Why I use two “for” ?! Delete one “for”.

function mutation(arr) {
arr = arr.join(" “).toLowerCase().split(” ");

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

return true;
}

1 Like