如有错误,请指导,改正
// 利用switch语句实现
var y = parseInt(prompt("请输入一个年份"));
var m = parseInt(prompt("请输入一个月份"));
var d = parseInt(prompt("请输入一个天数"));
var sum = 0;
switch (m - 1) {
case 11: sum += 30;
case 10: sum += 31;
case 9: sum += 30;
case 8: sum += 31;
case 7: sum += 31;
case 6: sum += 30;
case 5: sum += 31;
case 4: sum += 30;
case 3: sum += 31;
case 2: sum += 29;
case 1: sum += 31;
if (!(y % 4) && y % 100 || !(y % 400)) {
console.log(sum + d)
} else {
console.log(sum + d - 1)
}
}
//利用for循环实现
var y = parseInt(prompt("请输入一个年份"));
var m = parseInt(prompt("请输入一个月份"));
var d = parseInt(prompt("请输入一个天数"));
function result(y, m, d) {
var sun = 0;
for (var i = m; i >= 1; i--) {
if (i == 2 || i == 4 || i == 6 || i == 8 || i == 9 || i == 11) {
sun += 31;
} else if (i == 5 || i == 7 || i == 10 || i == 12) {
sun += 30;
} else if (i == 3) {
sun += 28;
}
if (i == 1) {
sun += d;
}
}
if (!(y % 4) && y % 100 || !(y % 400)) {
if (m > 1) {
return sun + 1
}
} else {
return sun
}
}
console.log(result(y, m, d))
//利用for循环和arguments
let y = parseInt(prompt("请输入年份"));
let m = parseInt(prompt("请输入月份"));
let d = parseInt(prompt("请输入天数"));
var sun = 0;
function result(y, m, d) {
for (i = 1; i < m; i++) {
sun += arguments[i + 2];
}
if (!(y % 4) && y % 100 || !(y % 400)) {
return sun + d
} else {
return sun + d - 1
}
}
console.log(result(y, m, d, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31))