dimanche 29 août 2021

Why is my if statement not working javascript?

I've created an If statement to return a boolean if a word has to of the same consecutive letters, but my code is only returning false and idk why

const doubleLetters = word => {
   let letters = word.split('')

   for(let i = 0; i < letters.length; i++){
      if (letters[i] === letters[i + 1]){
         return true
      } else{
         return false
      }
   }
}

could someone assist me? I've compared my code to

function doubleLetters (word) {
   const letters = word.split('') 
   for (let index = 0; index < letters.length; index++) {
     
     if (letters[index] === letters[index + 1]) {
       return true
     }
   }
   return false
 }

which appears to be working correctly. It looks like my code, but maybe I'm missing something since mine is broken. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire