0

I'm using an else if statement to tell me the current season (In the Southern Hemisphere)

const MONTH = 'March'

if (MONTH == 'December'|| MONTH == 'January'|| MONTH == 'February'){
   console.log('Summer')
}
else if (MONTH == 'March'|| MONTH == 'April'|| MONTH == 'May'){
  console.log('Autumn')
}
else if (MONTH == 'June'|| MONTH == 'July'|| MONTH == 'August'){
  console.log('Winter')
}
else console.log('Spring')

While this works, the repeated use of MONTH is not a great way to go about it.

What would be a better way to write this code, still using an if else statement?

  • I don't see anything specifically wrong with this, although I'm a fan of arrays, so I'd probably do something like `if("December","January","February].includes(MONTH)){...}` – mykaf Sep 12 '22 at 21:07
  • Use a switch statement `switch (month) { case 'December': case 'January': case 'February': console.log('a'); break; case '': .... }` or `if (['December','January','February'].includes(month)){} else if....` – epascarello Sep 12 '22 at 21:08
  • Thanks @mykaf, I feel like it's a bit of an easier way to read the code writing it like that. – fly_sprig117 Sep 13 '22 at 18:53

0 Answers0