codewars练习(javascript)-2021/2/28

codewars-js练习

2021/2/28

github 地址

my github地址,上面有做的习题记录,不断更新…

【1】<6kyu>【Determine the date by the day number】

What date corresponds to the nth day of the year?
The answer depends on whether the year is a leap year or not.

Write a function that will help you determine the date if you know the number of the day in the year, as well as whether the year is a leap year or not.
The function accepts the day number and a boolean value isLeap as arguments, and returns the corresponding date of the year as a string "Month, day".
Only valid combinations of a day number and isLeap will be tested.

example

getDay(41, false)   =>  "February, 10"  //  41st day of non-leap year is February, 10
getDay(60, false)   =>  "March, 1"      //  60th day of non-leap year is March, 1
getDay(60, true)    =>  "February, 29"  //  60th day of leap year is February, 29
getDay(365, false)  =>  "December, 31"  //  365th day of non-leap year is December, 31
getDay(366, true)   =>  "December, 31"  //  366th day of leap year is December, 31

solution

<script type="text/javascript">
        function getDay(day, isLeap){
            // console.log(day,isLeap);
            /*
            先根据isLeap判断是否为闰年,若为闰年,则2月为28天
            反之2月为29天。从而判断当前为哪一天
            */
            let days_of_year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
            let temp = [31,59,90,120,151,181,212,243,273,304,334,365]
            if(isLeap){
                days_of_year[1] = 29
                let temp2 = [31,60,91,121,152,182,213,244,274,305,335,366]
                let result = monthDay(day,temp2,days_of_year);
                return result

                }else{
                    let result = monthDay(day,temp,days_of_year);
                    return result
                }
        }
        function monthDay(day,arr,days_of_year){
            let info = {1:'January',2:'February',3:'March',4:'April',5:'May',6:'June',7:'July',8:'August',9:'September',10:'October',11:'November',12:'December'};
            for(let i=0;i<12;i++){
                    if(day <=arr[i]){
                        let month = i+1;
                        date = days_of_year[i]-(arr[i] - day);
                        // console.log('month',info[month])
                        // console.log('date',date)
                        return info[month] + ', ' + date
                    }
                }
        }

        // 验证
        console.log(getDay(15, false));//"January, 15"
        console.log(getDay(41, false));//"February, 10"
        console.log(getDay(59, false));//"February, 28"
        console.log(getDay(60, false));//"March, 1"
        console.log(getDay(366, true));//"December, 31"
        console.log(getDay(365, true));//"December, 30"
        console.log(getDay(1, true));//"January, 1"
        console.log(getDay(112, false));// April, 22
	</script>

以上为自己思路供大家参考,可能有更优的思路。

周末愉快

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值