-3

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

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Sammi
  • 1
  • 2
  • _"It looks like my code"_ - Not quite. Your code `return`s in the `if` and `else` branch of the loop. And `return` stops the execution of the function it is in - and therefor your loop with the first letter. The working version only returns `true` in the `if` branch. The `false` case is after the loop at the end of the function. – Andreas Aug 29 '21 at 11:54
  • at the end `index + 1` is out of range – Mister Jojo Aug 29 '21 at 11:54
  • Possible dupe: [Does return stop a loop?](https://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – Andreas Aug 29 '21 at 11:55

3 Answers3

1

Andreas's comment is right, this not the same code, there is no else { return false } in the original version, this return false is out of the loop.

Actualy your loop stop after the first step, or doesn't return anything if the input argument (word) is empty

there is also a small error about

if (letters[index] === letters[index + 1]) {

at the end of the string index +1 is out of range

a correct code should be:

const doubleLetters = word =>
  {
  const letters = word.split('') 
  for (let index = 1; index < letters.length; index++)
    { 
    if (letters[index] === letters[index - 1])
      {
      return true
      }
    }
  return false
  }
  
console.log( doubleLetters('abxxef'))
console.log( doubleLetters('abcdef'))

or, if you want a complete ES6 coding, change your loop type,
and keep a previous letter before:

const doubleLetters = word =>
  {
  let previous = null
  
  for (let letter of word)
    {
    if (previous === letter) return true
    previous = letter
    }
  return false
  }
  
console.log( doubleLetters('abxxef'))
console.log( doubleLetters('abcdef'))

Or even shorter, with Array.some(), Spread syntax (...), and other things

const doubleLetters = w => [...w].some((l,i,{[i+1]:n})=>l===n)

const doubleLetters = word => [...word].some((letter,i,{[i+1]:next})=>letter===next)  

console.log( doubleLetters('abxxef'))
console.log( doubleLetters('abcdef'))

for info, if you absolutely want to test each letter of the word and their next, you will have to use a flag to summarize the answers

const doubleLetters = word =>
  {
  let
    test = false
  , letters = word.split('')
    ;
  for(let i = 0; i < letters.length; i++) 
    {
    test ||= (letters[i] === letters[i + 1])
    }
  return test
  }

console.log( doubleLetters('abxxef'))
console.log( doubleLetters('abcdef'))
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • If you use a snippet then please also use the _"Tidy"_ feature (unless the unusual scheme is really as intentional as it is). – Andreas Aug 29 '21 at 12:12
  • @Andreas It is intentional, I only use the Whitesmiths style. In my opinion the K&R style is not advisable, especially for beginners – Mister Jojo Aug 29 '21 at 12:21
0

Remove the else part in your code. Your code will return true only if first 2 character s are same, otherwise else part will be executed and false will be returned.

In the other function code return false is outside the loop.

Subhashis Pandey
  • 1,473
  • 1
  • 13
  • 16
0

Your main issue is the return false placement. Let's first walk through the working code and check what it does:

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

The above code checks if a letter is the same as a consecutive letter. If it is equal, true is returned. If it is not equal we'll move on to the next letter. If all letters are checked and no match has been found then return false.

const letters = ["a", "b", "b", "c"];
// executes as
"a" === "b" // not equal, next!
"b" === "b" // equal, return true

const letters = ["a", "b", "c"];
// executes as
"a" === "b" // not equal, next!
"b" === "c" // not equal, next!
"c" === undefined // not equal, next!
// all letters have been checked (for-loop is complete), return false

Now let's have a look at your code:

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

The above code checks if a letter is the same as a consecutive letter. If it is equal, true is returned. If it is not equal, false is returned. This means that the function is always going to return in the first iteration. The other letters are never checked.

const letters = ["a", "b", "b", "c"];
// executes as
"a" === "b" // not equal, return false

const letters = ["a", "b", "c"];
// executes as
"a" === "b" // not equal, return false

return is a statement that marks the end of the function. Nothing (within the same function) will be executed after a return statement, therefore the second iteration of the for-loop will never be reached and other letters are never checked.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52