wyc
wyc

Reputation: 55273

Why is the following string being console-logged twice in the following if-else statement?

I'm looping through an array of strings to check the current and previous string (and matching their contents):

const sentences = [
  'Manabu had been standing in front of this convenience store for almost half an hour',
  'His feet shuffling',
  'His heart rattling',
  "He wasn't going to rob the cash register",
  'Or hit on the clerk',
  'This feat would be more embarrassing than all that',
  'Floating in this feeling, his lips curled up, he slid on his slim jeans and ambled out his apartment'
]

const result = sentences.forEach((sentence, index, array) => {
  const prevSentence = array[(index + array.length - 1) % array.length]
  const nextSentence = array[index + 1]
  const character = {
    name: 'Manabu',
    pronouns: /\b(he|him|his|himself)\b/i
  }

  if (index === array.length - 1) { // Last sentence
    console.log('CURR0:', sentence)
  } else if (prevSentence.match(character.pronouns) && sentence.match(character.name)) {
    console.log('PREV1:', prevSentence)
    console.log('CURR1:', sentence)
  } else {
    // do something
  }
})

I thought the last string Floating in ... would only be consoled-logged as CURR0: Floating in ..., since I'm using else-if. But it's being consoled-logged as PREV1: Floating in ... as well.

Why is this? And how to fix it?

Upvotes: 0

Views: 28

Answers (1)

esqew
esqew

Reputation: 44702

It's because the sentence beginning with Floating in ... is tagged by the logic as being the prevSentence for the sentence at index 0 (beginning with Manabu had...). This is due to your use of the modulus operator %, which ensures that if index === 0, the code will look at the last element of the array and use that as the prevSentence.

If you don't want this to happen, adjust your logic to calculate prevSentence as you've done with nextSentence with simple arithmetic (index - 1). You'll need to include a bit of a "fail-safe" to ensure that your code doesn't throw an exception when processing the first item (array[-1] isn't valid and will cause issues when trying to call methods on it); I've done so below using the ternary operator syntax.

const sentences = [
  'Manabu had been standing in front of this convenience store for almost half an hour',
  'His feet shuffling',
  'His heart rattling',
  "He wasn't going to rob the cash register",
  'Or hit on the clerk',
  'This feat would be more embarrassing than all that',
  'Floating in this feeling, his lips curled up, he slid on his slim jeans and ambled out his apartment'
]

const result = sentences.forEach((sentence, index, array) => {
  const prevSentence = index !== 0 ? array[index - 1] : ''
  const nextSentence = array[index + 1]
  const character = {
    name: 'Manabu',
    pronouns: /\b(he|him|his|himself)\b/i
  }

  if (index === array.length - 1) { // Last sentence
    console.log('CURR0:', sentence)
  } else if (prevSentence.match(character.pronouns) && sentence.match(character.name)) {
    console.log('PREV1:', prevSentence)
    console.log('CURR1:', sentence)
  } else {
    // do something
  }
})

Upvotes: 2

Related Questions