Basic Algorithm Scripting - Mutations

Tell us what’s happening:
Hey everyone!
So, as I was working through this problem and trying to find alternate solutions, I came across something interesting which I don’t understand.

Please review the code below and help me understand why this does not work. From my thinking the logic should work, but the {mutation([“hello”, “hey”])} test keeps returning true!?!

I’m lost…

Your code so far
function mutation(arr) {

//creates a var for index 0 and 1 which sets them to lowercase and converts to array
let x = arr[0]
.toLowerCase()
.split(“”);
let y = arr[1]
.toLowerCase()
.split(“”);

  //should be able to test x for all of var y

return x.includes(…y);
}

mutation([“hello”, “hey”]);

console.log(mutation([“hello”, “hey”]))
//should return false, but returns true
//this code passes all other tests in this section except [“hello”, “hey”]

function mutation(arr) {

  //creates a var for index 0 and 1 which sets them to lowercase and converts to array
  let x = arr[0]
      .toLowerCase()
      .split("");
  let y = arr[1]
      .toLowerCase()
      .split("");

      //should be able to test x for all of var y
  return x.includes(...y);
}

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

console.log(mutation(["hello", "hey"]))
//should return false, but returns true
//this code passes all other tests in this section except ["hello", "hey"]

Your browser information:

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

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

array.includes(searchElement)
array.includes(searchElement, fromIndex)

Why do you think you can pass the whole array as arguments to the includes method?
And assuming that doesn’t work, what does your function currently test for?

I am trying to test each element for array ‘y’ to see if it is present in array ‘x’. I thought I could use ‘…y’ to iterate through each element of ‘y’ for the test. It seems to work for every case except for the one indicated.

cases tested:
mutation([“hello”, “hey”])
mutation([“hello”, “Hello”])
mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”])
mutation([“Mary”, “Army”])
mutation([“Mary”, “Aarmy”])
mutation([“Alien”, “line”])
mutation([“floor”, “for”])
mutation([“hello”, “neo”])
mutation([“voodoo”, “no”])
mutation([“ate”, “date”])
mutation([“Tiger”, “Zebra”])
mutation([“Noel”, “Ole”])

Most of the function in javascript don’t care how many arguments you pass in. :slightly_smiling_face:

You assume that the iteration works for every other case, but maybe that’s not the case and there is another reason for this behaviour?

If you can’t find it:
what’s different:

the first test case is the only one where the first letter of the second word is in the first word AND there is later letter that’s missing

conclusion:

the function only tests if the first letter of the second word is in the first word, and returns that → rewrite it so it really tests every letter

1 Like

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