我在打代码战,这是一个挑战:
给出一个月从1到12之间的整数,返回到一年中的哪个季度,它属于一个整数。
例如:第二个月(二月)是第一季的一部分;第六个月(六月)是第二季的一部分;第十一个月(十一月)是第四季的一部分。
以下是我尝试过的:
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
}
}这似乎不起作用,我知道我每个月都可以给一个变量赋值,但我正在努力提高我的技能,有人能解释一下为什么这个不适用于我吗?
发布于 2022-11-26 22:34:59
这很简单,您试图在一条语句中检查两个条件,尝试使用&为分离条件,您的代码如下所示:
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
}
}
发布于 2022-11-26 22:47:57
你所需要的就是
const quarterOf = (month) =>
{
if (month <= 3) return 1
if (month <= 6) return 2
if (month <= 9) return 3
return 4
}或
const quarterOf = month => Math.ceil(month / 3);https://stackoverflow.com/questions/74586033
复制相似问题