My Mutation Doesn't output right

Tell us what’s happening:
All instances return correctly, except [“hello” , “hey”], this string always returns true.

Your code so far
function mutation(arr) {
“use strict”;
for(let i=0; i<=arr[0].length; i++)
return (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split("")) !== -1);
console.log(mutation([“hello”, “hey”]))
mutation([“hello”, “hey”])

function mutation(arr) {
"use strict";
for(let i=0; i<=arr[0].length; i++)
return (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split("")) !== -1);
console.log(mutation(["hello", "hey"]))
mutation(["hello", "hey"])

Your browser information:

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

Challenge: Mutations

Link to the challenge:

Your function will stop as soon as the first return statement is encountered, which means that your loop will only run once.

1 Like

What can I do to solve this?

Well, your return statement needs to be outside of your loop.

I like the idea of this test

arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split("")) !== -1

Lets back up a bit. In words, when do you need to return true and when do you need to return false from this function?

If my 1st string contains all letters from 2nd string, it should return true, else it should return false.

Right, so this means that you should only return true after the entire loop has completed.

But you should return false the first time you find a letter that does not match.

Does that give you an idea about what you should do with this

arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split("")) !== -1

I tried declaring a variable inside the For loop & initializing it with this value
arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split(“”)) !== -1
But it shows toLowerCase is undefined.
here is the code:

for(let i=0; i<=arr[0].length; i++){

  const result=(arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase().split("")) !== -1);


return result;

This will have two issues:

  • This variable will only be defined within the nearest set of {}, which means it is only defined inside of the loop

  • You are overwriting this variable each time, so you will only have the result from the last character in the first string.

If you find a letter that is not present, then you should immediately return false.

Thank You.
I have completed the challenge using this code:

var arr1 = arr[0].toLowerCase().split(‘’);
var arr2 = arr[1].toLowerCase().split(‘’);

for (var i=0; i < arr2.length; i++) {

if(arr1.indexOf(arr2[i]) == -1 ) {

  return false;

return true;

I understand now, but still when I tried doing it in short, it was showing some errors.
But still thank you for co-operating Sir.

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