Mutations Code Review

Mutations Code Review


Hi all my code works with 4 attempts. Looking for any reviews that maybe helpful to understand why it’s not working with the rest of the attempts.

ie I don’t understand why mutation([“floor”, “for”]) does not return true; while mutation([“Mary”, “Army”]) does return true.

Your code so far{ return x.toLowerCase() })
     var elem0 = arr[0]; 
     var elem1 = arr[1]; 
     var arrElem1 = elem1.split("");
     var arrIndex = [];
     for (i = 0; i < arrElem1.length; i++) {
        var arrSubElem1 = arrElem1[i];
        var arrSubIndex = elem0.indexOf(arrSubElem1);
        return false;} else {
            return true;}

mutation(["floor", "for"])  

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:


Your solution has a couple of issues.

#1) The following code, does not do what you think it is doing. {
		return x.toLowerCase();

The map function does not mutate the original array (arr). The map function returns a new array which needs to be assigned to a variable. Basically, arr is still the exact same array after the above code executes.

#2) Assuming you figure out how to correct the above code, the other issue is your if/else statement at the bottom:

	if (arrIndex.indexOf(-1)) {
		return false;
	else {
		return true;

What the above code does is evaluates if arrIndex.indexOf(-1) is true. Your algorithm centers around the idea that if arrIndex has an element with a value of -1, then a letter was not found. This is fine, but let’s use the example of mutation([“floor”, “for”]) In this case, arrIndex looks like [0, 2, 4], so arrIndex.indexOf(-1) returns -1. If statements evaluate to a true or false. You have not learned about truthy and falsy values (see the next challenge called Falsy Bouncer), but it just so happens that any value which is either false, null, 0, “”, undefined, and NaN will evaluate to false. Since -1 is not one of these values, it evaluates to true. So what you really want to do is compare the evaluation of arrIndex.indexOf(-1) to -1 itself. If the two are equal, then you know every letter was found. If it returns an index of 0 or greater, then you know that one of the elements in arrIndex had a -1 in it, so you know a letter was not found.