Here’s my annotated answer. I commented it so its self explanatory
brief explanation
- There’s 2 loops here, the first iterates through the arr[1], the second loop is through arr[0]
- No
indexOf
statements used here
function mutation(arr) {
//Lowercase array keys to store string vars
var sampleStr = arr[0].toLowerCase();
var checkStr = arr[1].toLowerCase();
//Bool condition if letters in "Hey" match "Hello"
var validateLetter = false;
//Test for failure
for (let i=0; i<checkStr.length; i++){
//Assume fail conditions per "h","e","y" checks
validateLetter = false;
//Check
for (let j=0; j<sampleStr.length; j++){
if (checkStr[i] == sampleStr[j]){
validateLetter = true;
}
}
//Stop execution when "y" in "hey" doesn't match any char in "Hello"
if (!validateLetter){
return false;
}
}
//If no failure then must be true
return true;
}
mutation(["hello", "hey"]);
modified with indexOf solution
After writing basic solution I modified it with indexOf
string method using same for loop / iteration logic
function mutation(arr) {
//Lowercase array keys to store string vars
var sampleStr = arr[0].toLowerCase();
var checkStr = arr[1].toLowerCase();
//Compare/Test for failure
for (let i=0; i<checkStr.length; i++){
if (sampleStr.indexOf(checkStr[i])==-1){
return false;
}
}
return true;
}
mutation(["hello", "hey"]);
Looking at official solutions this is more or less what basic solution looks like, except I called target == sampleStr
and test == checkStr