Basic Algorithm Scripting: Mutations - TypeError: unknown: Cannot read property '0' of undefined

Tell us what’s happening:
Hello everybody, I’ve come across this challenge and I get stuck, the browser keeps throwing this Error: TypeError: unknown: Cannot read property ‘0’ of undefined.
Could you please show me what the problem is in my code? Thanks so much.

Your code so far


function mutation(arr) {
  "use strict"
  let secondArray = arr[1].toLowerCase().split('');
  let firstArray = arr[0].toLowerCase().split('');

  for (let i = 0; i < firstArray.length; i++)
    if (secondArray.indexOf(firstArray[i]) === -1)
      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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/

You need to 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.
So exchange firstArray with secondArray and secondArray with firstArray in the for loop.

function mutation(arr) {
  "use strict"
  let secondArray = arr[1].toLowerCase().split('');
  let firstArray = arr[0].toLowerCase().split('');

  for (let i = 0; i < secondArray.length; i++)
    if (firstArray.indexOf(secondArray[i]) === -1)
      return false;

  return true;
}

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

It still throws the same error, I am really confused right now. Thanks for your help anyway bro :grinning:

I also wanted to know what is causing the error. I am getting the same error message, but in other challenge (https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-primes/).

If you create your own topic with the “Ask for help” button in the challenge we will be able to see the code and help in fixing the issues.

1 Like

Don’t forget to use {} after the second for