Didn't understand the solution very clearly

*Tell us what’s happening:
how does the function(letter) { return arr[0].toLowerCase().indexOf(letter) != -1; }
work inside the Array.prototype.every() function

Your code so far


function mutation(arr) {
return arr[1]
  .toLowerCase()
  .split("")
  .every(function(letter) {
    return arr[0].toLowerCase().indexOf(letter) != -1;
  });
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36.

Challenge: Mutations

Link to the challenge:

arr contains two strings, so arr[0] is for the first string and arr[1] is for the second string.

What you do actually is the following:
you take the second string (i.e. arr[1]) you change to lowercase, then you split it (you will have an array), and you use the method every, to check if every element of the array satisfies the function (the function return true or false), if yes, the mutation function will return true else it will return false.

what the function inside the every method does is simply, checks if the first string contains the letter, (the letter is a variable that changes because The every method executes the provided callback function once for each element present in the array).
How does the function check? it take the first string , turns it to lowercase (because uppercase is not the same as lowercase), and then checks the index of letter inside the first string using the method indexOf(you can use indexOf on strings), the indexOf method return “-1” if the element is not found, so it is like writing the function like that

function(letter) {
    return  (index of letter inside the first string) != -1;
// if (index of letter inside the first string) is not found (i.e -1), the expression becomes -1 != -1 which is false, then there is a letter missing, which means " the string in the first element of the array DOES NOT contain all of the letters of the string in the second element of the array"
  }

TL;DR: the function simply checks if letter is present in the first string.

1 Like

That solution is very…concisely…written. It’s short, and effective, and clear as mud as a learning tool.

So the first thing to understand is the code leading up to the every(...). That code takes the second string, converts it to a lowercase string, then turns that into an array of letters.

The function you’re asking about will be used on every letter in that array. .every(...) runs a function on each member of an array, and only returns true if they all return true. This is key. So the function inside there takes one letter, and checks if that letter is in the first string in arr. That’s all that function does - does this one letter occur in this string?

If that function returns true for the entire array of lowercase letters, then every returns true. If even one returns false, the while thing is false.

Does that help?

Often, when looking at a challenge like this, i try to simplify. “What if, instead of an array of things to test, i want to test one thing?” That’s easier to grok, and coding that can often be used as a small part of a bigger structure - an individual Lego in your castle.