# Basic Algorithm Scripting - Mutations

Tell us what’s happening:

I wasn’t able to understand, how the commented part works !??
I want some detailed explanation of that line.

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

console.log(mutation(["hello", "hey"]));
``````

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

Challenge: Basic Algorithm Scripting - Mutations

Hey there,

let’s look at it step by step.

When we call `mutation(["hello", "hey"])`

``````const test = arr[1].toLowerCase(); // hey
const target = arr[0].toLowerCase(); // hello
for (let i = 0; i < test.length; i++) {
if (target.indexOf(test[i]) < 0) {
return false;
}
}
return true;
``````
``````target === 'hello'
``````

when `i = 0` => `test[0] === 'h'`

``````'hello'.indexOf('h') === 0
``````

when `i = 1` => `test[1] === 'e'`

``````'hello'.indexOf('e') === 1
``````

when `i = 2` => `test[2] === 'y'`

``````'hello'.indexOf('y') === -1
``````

When `indexOf()` cannot find the value it’s looking for, it returns `-1`.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.