Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

หาไตรมาสจากดัชนีเดือนใน JavaScript


ปัญหา

เราต้องเขียนฟังก์ชัน JavaScript ที่รับดัชนีเดือน (1-based) และคืนไตรมาสที่เดือนนั้นอยู่

ตัวอย่าง

โค้ดด้านล่างนี้

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));

ผลลัพธ์

ผลลัพธ์ในคอนโซลคือ

3