Basic Algorithm Scripting: Mutations

Hi y’all!

I’m in the middle of some of the basic algorithm scripting problems and this one is giving me some trouble.

Here’s the prompt:

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.

For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments ["hello", "hey"] should return false because the string hello does not contain a y.

Lastly, ["Alien", "line"], should return true because all of the letters in line are present in Alien.

And here’s my code so far:

function mutation(arr) {
  const newArr = arr;
  for (let i = 0; i < newArr[1][i].length; i++) {
    console.log(newArr[1][i]); //this is temporary to test things out
    let testing = newArr[0].toLowerCase().includes(newArr[1][i].toLowerCase());
    if (testing == false) {
      return false;
      //this weeds out cases where there's a letter in newArr[1] that's not in newArr[0] and is case insensitive
    };
  }; return true;
};

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

In the test case, everything is passing except for this first test case, where: mutation(["hello", "hey"]) should return false .

Based off the console.log() I think it’s that the code isn’t properly running through the for loop but I’m not sure. I’m also suspecting that I’m not properly making a copy of arr with the newArr declaration, and that I might need to do this instead:

const newArr = [];`
newArr.push(arr);

(But doing so makes all the test cases fail.)

If anyone has any suggestions please let me know!

Cheers,

Jules

The goal of that line was to iterate through each letter in the second element in the array, so that I could check to see if that letter would appear in the first element in the array. It might help instead to use a method (I think split() might be one?) instead of doing it that way. It wouldn’t surprise me if this were the faulty line of code because the console.log() was only displaying the first letter in the second array element.

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