Basic Algorithm Scripting - Mutations

Tell us what’s happening:
all my cases are getting verified except, ```
mutation([“voodoo”, “no”])

it’s returning true when it’s supposed to written false, my counter is taking into account those extra o’s and that’s passing the condition even there’s no “no” in “voodoo”, how do I go about this

Your code so far

function mutation(arr) {
  const s1 = arr[0].toLowerCase();
  const s2 = arr[1].toLowerCase();
  const s1Split = s1.split("");
  const s2Split = s2.split("");
  let count = 0;
  for(let i=0; i<s2Split.length; i++) {
    for(let j=0; j<s1Split.length; j++) {
      if(s2Split[i]===s1Split[j]) {
        count++;
      }
    }
  }
  if(count>=s2Split.length) {
    return true
  }
  return false
}

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

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

Counting the number of timess2Split[i]===s1Split[j] is true is not the correct approach to this problem because of the very issue you are seeing with mutation(["voodoo", "no"]). One approach that would use the nested for loop structure you have is for each letter being iterated over of the second argument to use a Boolean flag variable initialized to false within the first for loop. Then, within the nested for loop if s2Split[i]===s1Split[j] is true, you know you found one of the letters, so you can set the flag variable to true.
After the nested for loop completes, you check if the flag variable is false. If it is, then you can go ahead and return false, since you know one of the letters was not found. After both for loops have completed, you know every letter was found because the function never returned false (and exited), so you can simply return true at the end of the function.

Omg thank you so much, I literally would’ve never thought of using boolean values to keep track of the condition, that’s smart and awesome, thank youu

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