Mutations - what I am missing?


#1

Hello campers, I am working on Mutations challenge and it doesn’t work… I don’t want to look at the solution, can you help me, please?

This is my code with the comments:

function mutation(arr) {
  var x = 0;
  for (var firstArray= 0; firstArray < arr[0].length; firstArray++) {  
    for (var secondArray = 0; secondArray < arr[1].length; secondArray++)  {
   
      x = arr[0][firstArray].indexOf(arr[1][secondArray]) !== -1;  // arr[0][firstArray] gets every letter of arr(0), then  .indexOf(arr[1][secondArray]) should get every letter of arr[1]...
      }
    }
  return x;
}

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

and without the comments:

function mutation(arr) {
  for (var firstArray= 0; firstArray < arr[0].length; firstArray++) {
    for (var secondArray = 0; secondArray < arr[1].length; secondArray++)  {
   
      x = arr[0][firstArray].indexOf(arr[1][secondArray]) !== -1;
      }
    }
  return x;
}

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

Link to the challenge: Mutations]

Thank you.


#2

You will have to change your code inside the for loops.
Basically the statement x=arr[...... only checks if the firstArray letter is equivalent to the secondArray letter.

h.indexOf(h)!==-1 x=true
h.indexOf(e)!==-1 x=false
h.indexOf(y)!==-1 x=false
.
.
.
o.indexOf(h)!==-1 x=false
o.indexOf(e)!==-1 x=false 
o.indexOf(y)!==-1 x=false

calculating all these goes into vain because at the end x value depends only on the comparison of the last character from both the array elements which does not meet the logic of the program.

I know i have framed the answer very poorly but if someone could take a hint from my answer and explain it better i would be grateful to them !


#3

function mutation(arr) {
var x = 0;//first you should arr[0] and arr[1] to a lowercase
for (var firstArray= 0; firstArray < arr[0].length; firstArray++) { //you don’t need to loop through arr[0]
for (var secondArray = 0; secondArray < arr[1].length; secondArray++) {//just loop through arr[1]

  x = arr[0][firstArray].indexOf(arr[1][secondArray]) !== -1;  //you must check arr[1][secondArray] in arr[0] not arr[0][firstArray] 

newArr=newArr.concat(x);// you should add a newArr and push x to
}
}
//add this code
x=(newArr.indexOf(true) !== -1) ? true : false ;
return x;
}

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