Finding Quarter Based on Month Index in JavaScript



Problem

We are required to write a JavaScript function that takes in the 1-based month index and return the quarter, which the month falls in.

Example

Following is the code −

 Live Demo

const month = 7;
const findQuarter = (month = 1) => {
   if (month <= 3) {
      return 1
   } else if (month <= 6) {
      return 2
   } else if (month <= 9) {
      return 3
   } else if (month <= 12) {
      return 4
   }
}
console.log(findQuarter(month));

Output

Following is the console output −

3
Updated on: 2021-04-17T11:00:13+05:30

591 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements