I am on codewars, here is the challenge:
Given a month as an integer from 1 to 12, return to which quarter of the year it belongs as an integer number.
For example: month 2 (February), is part of the first quarter; month 6 (June), is part of the second quarter; and month 11 (November), is part of the fourth quarter.
Here is what I tried:
const quarterOf = (month) => {
// Your code here
if (month <= 3) {
return 1
} else if (6 >= month > 3) {
return 2
} else if (9 >= month > 6) {
return 3
} else if (12 >= month > 9) {
return 4
}
}
This doesn't seem to work, I know I could assign each month a variable, but I'm trying to improve my skills, can someone explain why this does not work to me?
CodePudding user response:
This is simple you are trying to check for two conditions in one statement try to use && and || for separation of conditions your code will look like this:
const quarterOf = (month) => {
// Your code here
if (month <= 3) {
return 1
} else if (6 >= month && month > 3) {
return 2
} else if (9 >= month && month > 6) {
return 3
} else if (12 >= month && month > 9) {
return 4
}
}
CodePudding user response:
All you need is
const quarterOf = (month) =>
{
if (month <= 3) return 1
if (month <= 6) return 2
if (month <= 9) return 3
return 4
}
or
const quarterOf = month => Math.ceil(month / 3);
CodePudding user response:
Probably the simples solution unless you want 3x if
:
const quarterOf = (month) {
return Math.floor(month / 3 1);
}