Can I get suggestions on this assignment?
function mutation(arr) {
//console.log (arr[0] + " " + arr[1])
var test = arr[0].toLowerCase();
var target = arr[1].toLowerCase();
//console.log (test + " " + target)
for (var x=0; x<test.length; x++) {
//console.log(x + " " + target.indexOf(test[x]))
if (target.indexOf(test[x]) < 0) {
console.log("These words do not match");
return false;
}
}
return true;
}
mutation(["alien", "line"]);
Right now it’s passing most of the tests except for 3, 6, and 7. This is my understanding of the code:
-It takes each value from the array and converts it to lowercase in a new variable.
-A loop runs for the length of the Test variable.
-I’m a little bit confused by the If statement. I think it’s checking to see if the xth letter of Test is found in the Target variable.
-If the xth letter of Test is not found, a negative number is produced which breaks the loop and returns false.
-But if it goes through the whole length of Test without a problem, it returns true.
I guess I’m flummoxed why some of the cases (Mary & Army) are testing right, but other cases (Alien & line, floor & for) are testing wrong?