Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 −
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
Advertisements
