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: 17-Apr-2021

518 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements