codewars-js练习
2021/2/28
github 地址
【1】<6kyu>【Determine the date by the day number】
What date corresponds to the n
th 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>
以上为自己思路供大家参考,可能有更优的思路。
周末愉快