คำสั่งแบบมีเงื่อนไข - การกำหนดฤดูกาล

ฉันกำลังออกกำลังกายเพื่อกำหนดฤดูกาลตามวันที่กำหนดโดยเฉพาะ ด้านล่างนี้คือโค้ดของฉันและใช้งานได้ แต่ต้องมีวิธีที่ดีกว่านี้... ฉันต้องการใช้คำสั่งแบบมีเงื่อนไขสำหรับโซลูชันของฉัน อินพุตคือ:

  • เดือนเป็นสตริง
  • วันที่เป็นตัวเลข

วันที่เริ่มต้นฤดูกาล ฤดูใบไม้ผลิ 20 มีนาคม ฤดูร้อน 21 มิถุนายน ฤดูใบไม้ร่วง 22 กันยายน ฤดูหนาว 21 ธันวาคม

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมมาก!

let month = 'Dec';
let day = 23;

if(month === 'Apr' || month === 'May') {
    console.log('Spring');
    } else if((day >= 20 && month === 'Mar') || (day < 21 && month === 'Jun')) {
        console.log('Spring');
    } else if(month === 'Jul' || month === 'Aug') {
        console.log("Summer");
    } else if((day >= 21 && month === 'Jun') || (day < 22 && month === 'Sept')){      
        console.log("Summer");
    } else if (month === 'Oct' || month === 'Nov') {
        console.log("Autumn");
    } else if ((day >= 22 && month === 'Sept') || (day < 21 && month === 'Dec')) {
        console.log("Autumn");
    } else if (month === 'Oct' || month === 'Nov') {
        console.log("Winter");
    } else if((day >= 21 && month === 'Dec') || (day < 20 && month === 'Mar')){
        console.log("Winter");
    }


person Georgi Popov    schedule 25.09.2020    source แหล่งที่มา
comment
สิ่งนี้ตอบคำถามของคุณหรือไม่? การเข้ารหัส Javascript: ป้อนวันที่ที่ระบุ เอาต์พุตซีซัน   -  person Christian Baumann    schedule 25.09.2020
comment
ฉันได้ทำสิ่งนี้แล้ว และจากสิ่งที่ฉันเห็น โซลูชันที่ได้รับการยอมรับรวมถึงส่วนที่เหลือบางส่วน ไม่ได้รวมข้อมูลเกี่ยวกับวิธีการใช้การป้อนตัวเลขในโซลูชัน ฉันเห็นว่ามีวิธีแก้ไขปัญหาที่ซับซ้อนกว่านี้มาก แต่ดูเหมือนแตกต่างไปจากที่ฉันพยายามทำให้สำเร็จ :)   -  person Georgi Popov    schedule 25.09.2020
comment
@GeorgiPopov เลือกเดือนอย่างไร แบบฝึกหัดนี้ให้เดือนในรูปแบบสตริงหรือจะมีเมนูแบบเลื่อนลงที่คุณสามารถเลือกเดือนได้   -  person nick zoum    schedule 25.09.2020
comment
ขออภัย ฉันควรจะให้ข้อมูลนั้นตั้งแต่แรก เพียงรูปแบบสตริง.   -  person Georgi Popov    schedule 25.09.2020
comment
ตกลง - ฉันได้อัปเดตโค้ดของฉันโดยถือว่าเดือนเป็นสตริง 3 อักขระ และวันเป็นตัวเลข ฉันเพิ่งสร้างสตริงในรูปแบบ MMDD และทำการทดสอบเหมือนเมื่อก่อน   -  person ATD    schedule 25.09.2020


คำตอบ (3)


หากคุณสามารถแปลงวันที่ของคุณเป็นสตริงในรูปแบบ MMDD คุณสามารถทำได้:

var months = ["xxx","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function getMMDD(mth, dy) {
  return months.indexOf(mth).toString().padStart(2, "0") + dy.toString().padStart(2, "0");
}  

function findSeason(mth, dy) {
  let d = getMMDD(mth, dy);
  if (d < "0320") {return "Winter";}
  if (d < "0621") {return "Spring";}
  if (d < "0921") {return "Summer";}
  if (d < "1222") {return "Autumn";}
  return "Winter";
}

console.log(findSeason("Jan", 25));
console.log(findSeason("Feb", 25));
console.log(findSeason("Mar", 25));
console.log(findSeason("Apr", 28));
console.log(findSeason("May", 25));
console.log(findSeason("Jun", 25));
console.log(findSeason("Jul", 25));
console.log(findSeason("Aug", 25));
console.log(findSeason("Sep", 25));
console.log(findSeason("Oct", 25));
console.log(findSeason("Nov", 25));
console.log(findSeason("Dec", 25));

นี่เป็นเพียงการเปรียบเทียบสตริงง่ายๆ การทดสอบแรกที่ตรงกันจะส่งคืนฤดูกาล หากไม่มีการแข่งขันแบบทดสอบ (สองสามวันสุดท้ายของเดือนธันวาคม) ให้ส่งคืน Winter

person ATD    schedule 25.09.2020
comment
คุณจะไม่เพิ่มส่วน // convert to string in "MMDD" format เหรอ? - person nick zoum; 25.09.2020
comment
นั่นจะขึ้นอยู่กับวิธีการกำหนดส่วนของเดือนและวัน โดยอาจอยู่ในออบเจ็กต์ Date อยู่แล้ว หรือผู้ใช้สามารถเลือกได้ หรือได้มาจากสตริงข้อความ HTML ภายใน OP ทั้งหมดที่แสดงนั้นมีการระบุวันที่ ซึ่งอาจหมายถึงรูปแบบใดรูปแบบหนึ่งจากหลายๆ รูปแบบ หากทราบรูปแบบดั้งเดิมแล้ว ฉันสามารถเพิ่มสคริปต์การแปลงได้ - person ATD; 25.09.2020
comment
ถาม op เกี่ยวกับคำถาม แบบฟอร์มสตริงดังกล่าว - person nick zoum; 25.09.2020

คุณสามารถสร้างออบเจ็กต์ที่ประกอบด้วยแต่ละฤดูกาลแต่ละฤดูกาลสิ้นสุด จากนั้นค้นหาฤดูกาลแรกที่มีวันที่มากกว่าที่ระบุ (หากไม่พบ ให้วงกลมรอบวันที่ 1)

ปัญหาคือคุณต้องแปลงวันที่ 23 ธันวาคมเป็นวัตถุวันที่ วิธีง่ายๆ ในการทำเช่นนี้คือสร้างอาร์เรย์ของเดือนทั้งหมด และเมื่อใดก็ตามที่คุณต้องการค้นหาฤดูกาล ให้รับดัชนีของเดือนในอาร์เรย์นั้น และใช้ในลักษณะนี้ new Date(_year_, _index_, _date_)

var year = new Date().getFullYear();
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var seasons = {
  "Winter": new Date(year, 2, 20),
  "Spring": new Date(year, 5, 21),
  "Summer": new Date(year, 8, 22),
  "Autumn": new Date(year, 11, 21)
};

console.log(getSeason("Jan", 20));
console.log(getSeason("Feb", 20));
console.log(getSeason("Mar", 20));
console.log(getSeason("Apr", 20));
console.log(getSeason("May", 20));
console.log(getSeason("Jun", 20));
console.log(getSeason("Jul", 20));
console.log(getSeason("Aug", 20));
console.log(getSeason("Sep", 20));
console.log(getSeason("Oct", 20));
console.log(getSeason("Nov", 20));
console.log(getSeason("Dec", 20));
console.log(getSeason("Dec", 22));

function getSeason(month, day) {
  var index = months.indexOf(month);
  var newDate = new Date(year, index, day);
  for (var key in seasons) {
    if (newDate < seasons[key]) return key;
  }
  return Object.keys(seasons)[0];
}

person nick zoum    schedule 25.09.2020

มีอีกวิธีหนึ่งในการใช้เงื่อนไขที่ได้ผลเช่นกัน (ดูด้านล่าง) ขอบคุณมากสำหรับข้อเสนอแนะทั้งหมด!

let month = 'Apr';
let date = 3;

switch (month) {
    case "Mar":
        if (date >= 1 && date <= 19) {
            console.log("Winter");
        } else if (date >= 20 && date <= 31) {
            console.log("Spring");
            break;
        }
    case "Apr":
        if (date >= 1 && date <= 30) {
            console.log("Spring");
            break;
        }
    case "May":
        if (date >= 1 && date <= 31) {
            console.log("Spring");
            break;
        }
    case "Jun":
        if (date >= 1 && date <= 20) {
            console.log("Spring");
        } else if (date >= 21 && date <= 30) {
            console.log("Summer");
            break;
        }
    case "Jul":
        if (date >= 1 && date <= 31) {
            console.log("Summer");
            break;
        }
    case "Aug":
        if (date >= 1 && date <= 31) {
            console.log("Summer");
            break;
        }
    case "Sep":
        if (date >= 1 && date <= 21) {
            console.log("Summer");
        } else if (date >= 22 && date <= 30) {
            console.log("Autumn");
            break;
        }
    case "Oct":
        if (date >= 1 && date <= 31) {
            console.log("Autumn");
            break;
        }
    case "Nov":
        if (date >= 1 && date <= 30) {
            console.log("Autumn");
            break;
        }
    case "Dec":
        if (date >= 1 && date <= 21) {
            console.log("Autumn");
        } else if (date >= 22 && date <= 31) {
            console.log("Winter");
            break;
        }
    case "Jan":
        if (date >= 1 && date <= 31) {
            console.log("Winter");
            break;
        }
    case "Feb":
        if (date >= 1 && date <= 29) {
            console.log("Winter");
            break;
        }
};
person Georgi Popov    schedule 25.09.2020