dorseysen / One-Date-One-Question

this is a space for self-improvement of mine
1 stars 0 forks source link

2019-09-10:9月10日是教师节,也是马云告别阿里的日子,请写一个函数,返回距当下时间最近的节日,注意是将来。难度 ★★☆ #126

Open dorseysen opened 5 years ago

dorseysen commented 5 years ago

2019-09-10:9月10日是教师节,也是马云告别阿里的日子。 请写一个函数,返回距当下时间最近的节日,注意是将来。 难度 ★★☆

比如今天是教师节9月10日,则输出'教师节' 假如是7月2号,则输出将来最近的节日,即'建军节' 日历字典如下:

dorseysen commented 5 years ago

//  2019-09-10:9月10日是教师节,也是马云告别阿里的日子。
//  请写一个函数,返回距当下时间最近的节日,注意是将来。
//  难度 ★★☆

//  比如今天是教师节9月10日,则输出'教师节'
//  假如是7月2号,则输出将来最近的节日,即'建军节'
//  日历字典如下:

var festival = {
    "0101" : "元旦节",
    "0214" : "情人节",
    "0305" : "学雷锋纪念日",
    "0308" : "妇女节",
    "0312" : "植树节",
    "0401" : "愚人节",
    "0501" : "劳动节",
    "0504" : "青年节",
    "0601" : "国际儿童节",
    "0701" : "中国GD诞辰",
    "0801" : "建军节",
    "0910" : "教师节",
    "1001" : "国庆节",
    "1224" : "平安夜",
    "1225" : "圣诞节"
};

class getRecentFestival {

    constructor (festival) {

        this.festival = festival;

        this.festivalMap = Object.keys(this.festival);

        this.festivalMap.sort();

        this.init();
    }
    init () {

        let date = this.format(new Date());

        let arr = this.festivalMap.filter(item => item >= date);

        this.res = arr.length > 0 ? this.festival[arr[0]] : this.festival[this.festivalMap[0]];

    }

    format (date) {

        return this.fill(date.getMonth() + 1) + this.fill(date.getDate());
    }
    fill (date) {

        return date >= 10 ? date : '0' + date;
    }
}

return new getRecentFestival(festival);