Finding quarter based on month index in JavaScript

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 17-Apr-2021 10:59:49
Advertisements