Basic Algorithm Scripting - Mutations

I’m having an issue with part of this solution and hoping for some clarity.

The task: Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

The solution is listed below but I’m having issues understanding what this specific line means : if (target.indexOf(test[i]) < 0)

I cant figure out what 0 means here. It cant be that 0 means that an element of test[i] couldnt be found in the target variable, because if that were true then wouldn’t the line be :

if (target.indexOf(test[i]) < 1)
OR…
if (target.indexOf(test[i]) <= 0)

Probably stupid simple but Im so confused…

function mutation(arr) {
let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();
for (let i = 0; i < test.length; i++) {
  if (target.indexOf(test[i]) < 0) 
  return false;
}
return true;
}

mutation(["hello", "hey"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

indexOf returns a value of -1 if the value passed to it is not found in the array upon it is called. Checking for a value less than 0 means, the value was not found, hence the return of false. If indexOf returns a value of 0, that means the value was found in the array’s first index (remember arrays are zero-indexed).

1 Like

MDN is a good resource for looking up what methods do:

Also, its good to compare this solution to the logic in the solution you wrote to see how this lines up with the logic there.

The idea with these challenges is that you practice turning problems into code. That is a key skill to build in order to become a programmer. The idea with the posted solutions is that you can look up alternative approaches after you solve the problem.


I moved this to the ‘code feedback’ subforum since this is a working solution.

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Doh. I remember that now. Thank you!