I wonder how to return true or false depending on a condition result (line 23):
It should be like “if none of the items are equal, return false, otherwise, return true”.
If you use a loop, you can check something at each iteration, if that thing is false you can return false inside the loop and break from the function. If nothing is false then the loop ends and you can return true
It can also be used inverting the return statements depending on what you need to check.
This would be the structure: (blurred in case you want to figure it from the above explanation)
for (...) {
if (...) {
return false;
}
}
return true;
Using two nested loops would achieve the same thing.
In fact, my example and the example of using two loops is actually too slow. It would be O(n^2).
This example is O(n). Which would be significantly faster.
function mutation(arr) {
var validLetters = {};
for (let i = 0; i < arr[0].length; i = i + 1){
let letter = arr[0][i].toLowerCase();
validLetters[letter] = true;
}
for (let i = 0; i < arr[1].length; i = i + 1){
let letter = arr[1][i].toLowerCase();
if (!validLetters[letter]){
return false;
}
}
return true;
}