freeCodeCamp Challenge Guide: Mutations

freeCodeCamp Challenge Guide: Mutations
0

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

can someone please tell me how is this syntax correct?

if (target.indexOf(test[i]) < 0)
return false;

why is this not working (it gives me wrong ‘true’ on first of examples), while this has to be the right syntax :

if (target.indexOf(test[i]) < 0) {
return false;
}

Don’t expect anyone to help you when you only posted one if statement. You’re missing the rest of the code, and it seems like you only blindly copied and pasted the solution code anyway.

My solution code, I guess a bit over-complicated but working just fine.

function mutation(arr) {
  arr = arr.join(" ").toLowerCase().split(" ");
  let temp = 0;
  
  for(let i = 0; i < arr[1].length; i++) {
    if((arr[0].indexOf(arr[1][i]) >= 0)) {
      temp += 0;
    } else {
      temp += 1;
    }
  }
  if(temp > 0) {
    return false;
  } else {
    return true;
  }
  
}

mutation(["hello", "hey"]);

I found a similar solution using a counter to keep track of matches and then testing it against the length of the second string to return true or false.

function mutation(arr) { //break array into two separate strings that are lowercase. var first = arr.shift().toLowerCase(); var second = arr.shift().toLowerCase(); //record length of second string to later check against match counter. var secondCount = second.length; //split second string into an array to iterate through. var secondSplit = second.split(""); //load up the first letter to be checked. var secondLetter = (secondSplit.pop()); //create a counter to keep track of matches var count = 0; //iterate through secondSplit to check for first.indexOf matches. while (first.indexOf(secondLetter) > -1) { //if a match happens, add 1 to count. count++; //load up next letter to be checked. secondLetter = (secondSplit.pop()); //if the counter is the same number as the original second.length then return true. } return count === secondCount; } mutation(["Floor", "for"]);

I am n00b :expressionless:

Here’s my annotated answer. I commented it so its self explanatory

brief explanation

  • There’s 2 loops here, the first iterates through the arr[1], the second loop is through arr[0]
  • No indexOf statements used here

function mutation(arr) {
  
  //Lowercase array keys to store string vars
  var sampleStr = arr[0].toLowerCase();
  var checkStr = arr[1].toLowerCase();
  
  //Bool condition if letters in "Hey" match "Hello"
  var validateLetter = false;
  
  //Test for failure
  for (let i=0; i<checkStr.length; i++){
    
    //Assume fail conditions per "h","e","y" checks
    validateLetter = false;
    
    //Check
    for (let j=0; j<sampleStr.length; j++){
      if (checkStr[i] == sampleStr[j]){
        validateLetter = true;
      }
    }
    
    //Stop execution when "y" in "hey" doesn't match any char in "Hello"
    if (!validateLetter){
      return false;
    }
  }
  
  //If no failure then must be true
  return true;
}

mutation(["hello", "hey"]);

modified with indexOf solution

After writing basic solution I modified it with indexOf string method using same for loop / iteration logic

function mutation(arr) {
  
  //Lowercase array keys to store string vars
  var sampleStr = arr[0].toLowerCase();
  var checkStr = arr[1].toLowerCase();
  
  //Compare/Test for failure
  for (let i=0; i<checkStr.length; i++){
    if (sampleStr.indexOf(checkStr[i])==-1){
      return false;
    }
  }
  return true;
}

mutation(["hello", "hey"]);

Looking at official solutions this is more or less what basic solution looks like, except I called target == sampleStr and test == checkStr

there is another solution .:heart_eyes:

function mutation(arr) {
var s1,s2;

  s1=arr[0].toLowerCase().split("");
  s2=arr[1].toLowerCase().split("");
  var c=0;
  for(var i=0;i<arr[1].length;i++)
    {
      
      for(var j=0;j<arr[0].length;j++)
        {
          if(s2[i]==s1[j])
            {
              c++;
              break;
            }
          
          
        }
      
    }
   return (c>=arr[1].length);
   
  
}
mutation(["voodoo", "no"]);

Would you mind posting your full, current code you have at the moment? It’d help us further teach you :slight_smile:

No idea about efficiency in the code but ended up with this

function mutation(arr) {

var benchmarkString = arr[0].toLowerCase();
var checkString = arr[1].toLowerCase().split(’’);

return Math.min.apply(Math,checkString.map(function (counter) { return benchmarkString.indexOf(counter);})) >= 0 ;

}

MY solution :

function mutation(arr) {
var str0 = arr[0].toLowerCase().split("");
var str1 = arr[1].toLowerCase().split("");
var search = [];
for (var x = 0; x < str1.length ; x++){
search.push(str0.indexOf(str1[x]));
}
search.sort(function(a,b){return a-b;});
if (search[0] >= 0){
return true;
}else{
return false ;
}
}

function mutation(arr) {

for(var i in arr){
arr[i]= arr[i].toLowerCase(); // replace the the elements
}
var fs=arr[0].split(’’);
var ss=arr[1].split(’’);
var results=true;
for(var x in ss){ // how come they still teach i++ version isn’t this one easier?
if(fs.indexOf(ss[x]) == -1)
results = false;
}
return results;
}

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

was I thinking to much into this example because your code was very simple

My solution … surely could be cleaned up a little, but I am really happy that I passed :slight_smile:

My take:

function mutation(arr) {

var str1 = arr[0].toLowerCase();

var str2 = arr[1].toLowerCase().split(’’);

for(i = 0;i < str2.length;i++){
if(str1.indexOf(str2[i]) === -1) {

  return false;
}

}
return true;
}

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

function mutation(arr) {
  var flower = arr[0].toUpperCase();
  var slower = arr[1].toUpperCase().split('');
  return slower.every((c,i) => {
     return flower.indexOf(c) !== -1;
  });  
  
}

mutation(["hello", "Hello"]);

This was so hard for me, don’t know why, the test[i] part I couln’t put in code, anyway this is my solution:

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

}

mutation(["Alien", "line"]);

can someone tell me what’s wrong with my code?? I don’t know why it’s not working for the first mutation but is working for the rest.

 function mutation(arr) {
      var array = arr.join(',').toLowerCase().split(",");
  
  var compare = array[1].split("");

  for (var i = 0; i < array[1].length; i++) {
    if (array[0].indexOf(compare[i]) === -1) {
      return false;
    }
    
    else {
      return true;
    }

  }
  
}

mutation(["hello", "hey"]);

Hi campers . this is my code. Its not working with all tests, I could use some help to spot that bug.

function mutation(arr) {

for(j=0;j<arr.length;j++){
arr[j].toLowerCase();
b = [];
b = arr[1].split(’’);
for(i=0;i<b.length;i++){

result = arr[0].indexOf(b[i]);
if (result !== 0){
  return true;
}else{
  return false;
}

}
}
return arr;
}

I split arr[1] into letters, creating oneArr, then I used a while loop to add 1 to n as long as the letters from oneArr were present in arr[0]. So if the loop stops executing when n < oneArr.length ,it means one of the letters isn’t present and the function will return ‘false’.

function mutation(arr) {

var oneArr = arr[1].toLowerCase().split("");
var n=0;
while ((arr[0].toLowerCase().indexOf(oneArr[n]) > -1) && (n < oneArr.length)) {
n++;
}
if ((n < oneArr.length) === true) {
return false;
}
return true;
}

1 Like

Babu, creating that empty b variable isn’t necessary if you declare it in the next line. Still, you need to use var before introducing a new variable so that should go before b = arr[1].split(’’). I can’t imagine you’d have to use a double for loop for this task. Also, your final condition result !== 0 isn’t the right condition here. Read again about indexOf() and what the values it returns mean. Read the tips in the head of this thread, they helped me a lot.

[quote=“samnguyen94, post:69, topic:16025”]
var array = arr.join(’,’).toLowerCase().split(",");
[/quote] - I believe this line is unnecessary. You could just split arr into two simply indexing arr[0] and arr[1] and then change them toLowerCase. You use the name ‘array’ instead of ‘arr’ all throughout your code. ‘array’ is never declared here

Thanks , I did some reading, used var to declare an empty array, didn’t see the need to split but definitely lower cased my strings. This is what I came up with.

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

newArray.push(arr[i].toLowerCase());

}for(j=0;j<newArray[1].length;j++){
if (newArray[0].indexOf(newArray[1][j]) === -1){
return false;
}

}
return true;}.