Mutations freeCodeCamp Help needed

Mutations freeCodeCamp Help needed
0.0 0

#1

Tell us what’s happening:

Your code so far

function mutation(arr) {
  var str = arr[1];
  var main = arr[0];
  var arr1 = [];
  arr1 = arr[0].split("");
  //return arr1;
  var arr2 = [];
  arr2 = arr[1].split("");
  var count1;
  var count2;
  for(i= 0 ; i<arr[1].length;i++){
    if((arr1[i] !== arr2[i])){
      count1++;
    }
  }
  
  for(i= arr[1].length; i >0;i--){
    if((arr1[i] !== arr2[i])){
      count2++;
    }
  }
  if(count1 > 0 || count2 > 0){
    return true;
  }else {
    return false;
  }
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:

What improvements do i have to make ??


#2

Can you explain to us the algorithm you figured out that you based your code on? You do not seem to have a grasp on the actual steps it would take to solve the challenge.


#3

You should use the String.prototype.indexOf() function, it helps finding a char in a string.
And as said @randelldawson, use comments to explain what the code is doing :slight_smile:


#4

comparing every word of the two words


#5

That is not an algorithm. Plus, you don’t care if arr[0] has characters that are not in arr[1]. You only need to make sure the all the characters in arr[1] are present in arr[0]. For example, the following should result in true.

mutation(["abcdefghijklmnopqrstuvwxyz", "k"]);  // true because all the characters in the second element (i.e. "k") are present in "abcdefghijklmnopqrstuvwxyz"

but the following should not:

mutation(["hot", "abcdefg"]); // false because at least one of the second element's characters are not present in the word "hot".

#6

i have used substr() method and it also doesn’t help me i worked out for all except three test cases. P.S . I am trying to approach in a different way.:sweat_smile:


#7

Okay, I see the problem here,

you only check if char in arr1 are equal to char in arr2 one by one (first char equals first char, second char equals second char, …)
You need to check if first char of arr2 is equal to any char of arr1 (using indexOf will help for this)