Why my function mutation is not passing the test with this case ["hello","hey"]?

Hi. I am trying to solve an exercise where an array is provided. the array contain two strings. The exercise consists in verify if the letters of the second string of the array is included or not in the first string of the array., if it is the case return true and is is not it should return false. My function works wit all test case except one when the array provided is [“hello”, “hey”]. I want to know if it is something wrong with my code because I dont understand why it works with all the other cases except the mentioned.

  **This is the code**

function mutation(arr) {
let firstWord = arr[0].toLowerCase()
let secondWord = arr[1].toLowerCase()
let isIncluded = null

for (let i = 0; i < secondWord.length-1; i++) {
if (firstWord.includes(secondWord.charAt(i)) === true) {
return isIncluded = true
}
return isIncluded = false
}

return isIncluded
}

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





      **Your browser information:**

**Challenge:**  Mutations

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

Hi @IronDev, welcome to the forum.

Your code will never loop the entire second word, since it always returns.

You got a lot of correct mark mainly by luck.
Take this input for example:

mutation(["voodoo", "no"]) // returns false as 'n' is not in 'voodoo'
mutation(["voodoo", "on"]) // returns true as 'o' is in 'voodoo'

Hope this helps figuring out the problem :slight_smile:

1 Like

Second what Marmiz said. Also, in your for loop you use the less than sign AND you use length-1 (if you used the less than or equal to sign then you could have the -1) so it will never loop through the entire second word… it stops at the second-to-last letter.

Also, with the “neo” example, the first letter comes up as not being included in the first word and your isIncluded variable is set to false, but all the other letters in “neo” are found and isIncluded is the overwritten to be true, you’ll need to find a way to break out of that as well.

If you’re still having trouble tomorrow (April 28), you can send me a message and we can try to chat your way through this.

1 Like

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