Basic Algorithm Scripting - Mutations

What I am and am not understanding will hopefully be found evident enough within the code that made up this attempt…
Can someone help me understand what,

about the mechanics of my chosen method,

I have failed to properly comprehend?

Your code so far

function mutation(arr) {
  let pod1 = arr[0].toString()
  let pod2= arr[1].toString()
  let lettersP1 = pod1.split() 
  let lettersP2 = pod2.split()
  

  
  let testing = lettersP1.forEach(function(letter){
    return lettersP2.includes(letter)
   
  })
  return testing
    
     }
 

  

 


console.log(mutation(["hello", "hey"]))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

Every Individual component generates as desired… soooo my forEach understanding is incomplete…???
how?

function mutation(arr) {
  let pod1 = arr[0].toString()
  let pod2= arr[1].toString()
  let lettersP1 = pod1.split("") 
  let lettersP2 = pod2.split("")
  
  console.log(pod1,pod2,lettersP1, lettersP2)
  
  let testing = lettersP1.forEach(function(letter){
        console.log(letter)
        console.log(lettersP2.includes(letter))

    return lettersP2.includes(letter)
  })
  return testing
    
     }
 

  

 


console.log(mutation(["hello", "hey"]))

The forEach method doesn’t return a value (well, technically it returns undefined). So testing is going to equal undefined after the forEach is done and thus your function is always returning undefined.

so code structure IS solid???
… using wrong tool to execute?

Well, I think you are on the right path. This can definitely be solved with a simple loop. But I think you are using the wrong type of loop. You might want to consider a loop that will allow you to exit out of the function immediately the moment you determine that a letter in the second array is not in the first.

was literally reconstructing exactly that wayyy haha
is where i was at in process when i got this reply from you

 function mutation(arr) {
  let pod1 = arr[0].toString()
  let pod2= arr[1].toString()
  let lettersP1 = pod1.split("") 
  let lettersP2 = pod2.split("")
  let count = 0
    for (let i = 0; i < lettersP1.length; i += 1){
      let letter = lettersP1[i];

      if(lettersP2.includes(letter) === true){
        count ++;
        
        }
    }  
  //console.log(pod1,pod2,lettersP1, lettersP2)
 /* 
  let testing = lettersP1.forEach(function(letter){
 //       console.log(letter)
   //     console.log(lettersP2.includes(letter))
 console.log(lettersP2.includes(letter))

    return lettersP2.includes(letter)
  })
  */
  return testing
    
     }
 


/*
Failed:mutation(["hello", "neo"]) should return false.
Failed:mutation(["voodoo", "no"]) should return false.
Failed:mutation(["ate", "date"]) should return false.
Failed:mutation(["Tiger", "Zebra"]) should return false.
Failed:mutation(["Noel", "Ole"]) should return true.*/ 


console.log(mutation(["Mary", "Aarmy"])) 
console.log(mutation(["Alien", "line"]))
console.log(mutation(["floor", "for"]))
console.log(mutation(["hello", "hey"]))
console.log(mutation(["Mary", "Army"]))
console.log(mutation(["hello", "hey"]))

So let me ask you this. Why do you need to keep a count? As you are going through the for loop, you are looking to see if each letter in the second array is in the first array, right? If it isn’t, then you know the function should return false. So why can’t it just return false at that time. Do you really need to go through the rest of the letters in the second array when you know you are going to return false?

yea i was operating on a series of “semi-thought through” thoughts… that count thing was from a mostly erased skelaton of a scrapped build
im seeing it now…
and i didnt recall about for each and its return thingy
that

WHHHHYYYYYYYYY???

 function mutation(arr) {
  let pod1 = arr[0].toString().toLowerCase()
  let pod2= arr[1].toString().toLowerCase()
  let lettersP1 = pod1.split("") 
  let lettersP2 = pod2.split("")
    
  for (let i = 0; i < lettersP1.length; i += 1){
    let letter = lettersP1[i];
    
    if(lettersP2.includes(letter) != true){
      return false;
        }else
        {
          return true
        }
    }  
  //console.log(pod1,pod2,lettersP1, lettersP2)

    
  }
 


/*
(["voodoo", "no"]) should return false.
(["ate", "date"]) should return false.
(["Tiger", "Zebra"]) should return false.
(["Noel", "Ole"]) should return true.*/ 
console.log(mutation(["Alien", "line"]))
console.log(mutation(["floor", "for"]))
console.log(mutation(["hello", "hey"]))
console.log(mutation(["Mary", "Army"]))
console.log(mutation(["hello", "hey"]))
console.log(mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]))

You’ve got some logic issues in your code.

Remember, a return statement exits the function immediately. You can return false immediately if you know that specific letter in the second array is not in the first array. But can you return true immediately if it is?

Also, think carefully about which array you need to loop through and which array you should be checking? It might help if you gave your variables better names that describe their role.

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