Mutations Challenge, only 1 test not passing. Makes no sense

Mutations Challenge, only 1 test not passing. Makes no sense
0

#1

Tell us what’s happening:
I don’t get why the function call below is returning true, all other test for this challenge seem to pass.

Would really appreciate a pointer.

thanks

Your code so far

function mutation(arr) {
  arr[0] = arr[0].toLowerCase();
  arr[1] = arr[1].toLowerCase();
  for (i = 0; i < arr[1].length; i++) {
    if (arr[0].indexOf(arr[1][i]) === -1) {
      return false;
    }
    return true;
  }
}
  
mutation(["hello", "hey"]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

With this current logic:

    if (arr[0].indexOf(arr[1][i]) === -1) {
      return false;
    }
    return true;

In the first iteration of the for loop, you will return true unless arr[0].indexOf(arr[1][i]) === -1.
Once you issue the return statement, the function is exited and does not return to the for loop. That is why you only get one iteration.

So basically, if the first letter of arr[1] is not in arr[0], you return false, otherwise you return true. Since the first letter of “hey” is “h”, you return true and the function exits.


#3

Thanks. that helps me understand what’s going on. I have to get better at understanding what a for loop does in each iteration.

I should be able to figure it out now.


#4

Maybe this can help you. It’s not perfect but it work xD
It’s for loop and while together.

/*
Link: https://beta.freecodecamp.org/en/challenges/basic-algorithm-scripting/mutations
*/

/*
The goal of this code is to compare first-letters from the second array with the first one.
With the second array, you have to only one letter at a time with every other letters from the first array.
First of all, because this exercice is not case-sensitive, we have to put every letters of both arrays on the same pedestal.
Exemple (arr0 = ["Mary"] && arr1 = ["Army"]): arr1[0] which is 'A' will be compared to ["M"], ["a"], ["r"] and ["y"].
If it eventualy find the match between the two letters, it will change the comparaison letter from the first array and do the same again.
One important thing is if you end the do-while() loop with a 'false', it mean that you didn't find a match.
But because every letters from the second array must find a match to continue and ultimatly return 'true' as a result, you end the exercice
with a 'return false' because their is no need to continue.

More in depth:
These 2 lines give the same pedestal to compare out both array:
	let arr0 = arr[0].toLowerCase();
	let arr1 = arr[1].toLowerCase();
	
Then recover the length of both array. The first one will be use for searching match at each letters in the first array. It will give an
'escape' to leave the do-while() if there is a match.
The second will help to know exactly which letter to compare from the second array with the first one.
	let arr0L = arr[0].length;
	let arr1L = arr[1].length;

The rep variable will help use to know what boolean value the exercice will return at the end. We start with a default value 'true' and it utimatly
stay 'true' if every letters from the second array matched out the first array. At the end, if it's 'false', no match or enough match to complete the exercice.
	let rep = true;

The 'a' variable give the possibility of jumping from letter to letters in the first array. Because it change as much as the first array have letters in.
It's important to restore it to 0 when a has finished to do the loop comparaison again.
*/

function mutation( arr ) {
	let arr0 = arr[0].toLowerCase();
	let arr1 = arr[1].toLowerCase();
	let arr0L = arr[0].length;
	let arr1L = arr[1].length;
	let rep = true;
	
	for( let k = 0; k < arr1L; k++ ) {
		let a = 0;
		
		if( rep == false ) {
			return false;
		}
			
		do {
			
			if( arr1[k] == arr0[a] ) {
				rep = true;
				a = arr0L
			
			}else {
				rep = false;
				a++;
			}	
			
		}while( a != arr0L );

	}
	
	return rep;
};

console.log( mutation(["hello", "hey"]) ); //false
console.log( mutation(["hello", "Hello"]) ); //true
console.log( mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]) ); //true
console.log( mutation(["Mary", "Army"]) ); //true
console.log( mutation(["Mary", "Aarmy"]) ); //true
console.log( mutation(["Alien", "line"]) ); //true
console.log( mutation(["floor", "for"]) ); //true
console.log( mutation(["hello", "neo"]) ); //false
console.log( mutation(["voodoo", "no"]) ); //false