freeCodeCamp Challenge Guide: Mutations

freeCodeCamp Challenge Guide: Mutations
0
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;
  
}
1 Like
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;
}
3 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;
  }
      
}
1 Like

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.