Basic Algorithm Scripting - Mutations

Good day people.
so far i have passed a few tests but i seem to be failing the one with " Failed:mutation(["Alien", "line"]) should return true."

any hints would be appreciated.
Your code so far

function mutation(arr) {
const lower = [];

arr.forEach(element => {
lower.push(element.toLowerCase());
//making everything lowercase
});

let firstelement = lower[0];
let secondelemnt = lower[1];
//Seperating arrays into their own variable
for (let i = 0; i < arr.length; i ++){//iterating through array
if(secondelemnt.indexOf(firstelement[i]) < 0){//comparing the arrays
      return false;
}
return true;
}





};


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/104.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

Why are you iterating this array arr? What are the values of teh variables firstelement and secondelemnt?

The challenge says:

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.

And, how do you iterate a variable with a string value?

the values of firstelement will be the first array [0] in lower case .
reason for iteration is to excecate the condition which is the if statement for the amount of elements there are

You are iterating the array, arr. What is its length? And how many times you want to iterate?

What @prasadsaya means creating another array is not needed.

  const lower = [];

  arr.forEach(element => {
    lower.push(element.toLowerCase());
    //making everything lowercase
  });

  let firstelement = lower[0];
  let secondelemnt = lower[1];

Why not just use the toLowerCase method directly on arr[0] and arr[1] when assigning values to firstelement and secondelement and skip creating the extra array lower?

Also, regarding @prasadsaya’s second question, you might want to rethink iterating over arr as it only has two elements. The fact you passed any of the tests is pure coincidence.

1 Like

ok thank you so much for your input