1-14作业
如何对弹窗进行封装?然后将弹窗得到的值正确引入后面的function?
我的代码里没有能够实现封装弹窗
var year = prompt("请输入您的出生年份");
var month = prompt("请输入您的出生月份");
var date = prompt("请输入您的出生日期");
function cal(year, month, date) {
var formerdays = new Array();
formerdays[0] = 0;
formerdays[1] = 31;
formerdays[2] = 31 + 28;
formerdays[3] = 31 + 28 + 31;
formerdays[4] = 31 + 28 + 31 + 30;
formerdays[5] = 31 + 28 + 31 + 30 + 31;
formerdays[6] = 31 + 28 + 31 + 30 + 31 + 30;
formerdays[7] = 31 + 28 + 31 + 30 + 31 + 30 + 31;
formerdays[8] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
formerdays[9] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
formerdays[10] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
formerdays[11] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
var yearInt = parseInt(year);
var monthInt = parseInt(month);
var dateInt = parseInt(date);
if (isNaN(yearInt) || isNaN(monthInt) || isNaN(dateInt)) {
alert("请输入正确数字");
return;
} else {
var day = formerdays[Number(monthInt) - 1] + Number(dateInt);
if (yearInt % 4 == 0 && monthInt != 1 && monthInt != 2) {
day++;
}
return day;
}
}
document.write("您的生日在" + year + "的第" + cal(year, month, date) + "天");
正在回答
/** 零件① (功能模块①的零件)
* 函数名: judgmentMonth( bornMonth, februaryDate )
* 功能:判断月份的三种情况、并返回对应的月份最大天数。
* 参数1: bornMonth
* 说明: 出生月份
* 参数2: februaryDate
* 说明: 二月份最大天数
* 使用要求: bornMonth 1到12月
* */
function judgmentMonth( bornMonth, februaryDate ) {
//该变量用来存放 输入的【某年】【某月份】 能有多少天
var whatDay;
/**
* if来判断月份的三种情况。
* 相关信息:
* 一,三,五,七,八,十,十二:31天
* 四,六,九,十一: 30天
**/
if ( bornMonth == 1 || bornMonth == 3 || bornMonth == 5 || bornMonth == 7 || bornMonth == 8 || bornMonth == 10 || bornMonth == 12 ) {
return whatDay = 31;
} else if ( bornMonth == 4 || bornMonth == 6 || bornMonth == 9 || bornMonth == 11 ) {
return whatDay = 30;
} else if ( bornMonth == 2 ) {
//返回平年 or 闰年的二月份的天数(通过参数【februaryDate】给变量【whatDay】赋值)
return whatDay = februaryDate;
}
}
/** 功能模块①
* 函数名:judgmentDate(bornYear,bornMonth)
* 功能:计算输入的【某年】的【某月份】的最大天数,并返回
* 参数1: bornYear
* 说明: 出生年份
* 参数2: bornMonth
* 说明: 出生月份
* 使用要求: bornMonth 1到12月
**/
function judgmentDate(bornYear, bornMonth) {
//定义【某年】的二月的最大天数
var februaryDate;
//判断是润年 or 平年,并传入对应的【二月份参数】
//闰年规则:(4的倍数 and 不是100的倍数) or 400的倍数。
//二月:润年29天,平年28天。
if (bornYear % 4 == 0 && bornYear % 100 != 0 || bornYear % 400 == 0) {
//闰年的二月赋值
februaryDate = 29;
//使用 **零件①** :调用judgmentMonth()函数来判断[润年]的[某月]的最大天数,并返回
return judgmentMonth(bornMonth, februaryDate);
} else {
//平年的二月赋值
februaryDate = 28;
//使用 **零件①** :调用judgmentMonth()函数来判断[平年]的【某月】的最大天数,并返回
return judgmentMonth(bornMonth, februaryDate);
}
}
/** 功能模块② (内含功能模块①)
* 函数名:totalDay(bornYear, bornMonth, bornDate)
* 功能:计算【某年】的【某月】的【某天】是当年的第多少天,并返回
* 参数1: bornYear
* 说明: 出生年份
* 参数2: bornMonth
* 说明: 出生月份
* 参数3: bornDate
* 说明: 出生日期
* 使用要求:【 bornMonth 1到12月 】 and 【 bornDate 1到对应月份的最大天数内 】
**/
function totalDay(bornYear, bornMonth, bornDate) {
var sum = 0;
//调用功能模块① judgmentDate() 来计算【1月初到出生月份前几个月】的计算。
for (var i = 1; i < bornMonth; i++) {
sum += judgmentDate(bornYear, i);
}
bornDate = parseInt(bornDate);
return sum += bornDate;
}
/** 弹出框
* 函数名:allInput()
* 功能:计算【某年】的【某月】的【某天】是当年的第多少天
**/
function allInput() {
var bornYear = prompt("请输入您的出生年份");
var bornMonth = prompt("请输入您的出生月份");
//判断是否是1月到12月之间
if (bornMonth <= 12 && bornMonth >= 1) {
var bornDate = prompt("请输入您的出生日期");
//调用函数 judgmentDate(bornYear,bornMonth) 来获取 输入的年份月份的【最大天数】
//再进行判断 输入的天数是否正确。
if (bornDate <= judgmentDate(bornYear, bornMonth) && bornDate >= 1) {
document.write("您的生日在" + bornYear + "年是第" + totalDay(bornYear, bornMonth, bornDate) + "天");
} else {
//当输入的天数 不符合 指定年份月份的天数范围时,页面中输出提示。
document.write(bornYear + "年" + bornMonth + "月份" + "只有" + judgmentDate(bornYear, bornMonth) + "天," + "您输入的是" + bornDate + ",请不要写错");
}
} else {
//如果输入的月份是1月到12月【以外】的月份会输出下面的字符串
document.write("输入错误,请输入1-12月份以内");
}
}以上是放在js文件夹内javascript.js文件中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="js/javascript.js"></script> </head> <body> <script> allInput(); </script> </body> </html>
以上放入html文件中
供您参考。
如果能对您有帮助,请采纳。
帮你改好了,你看看!
function inp(){
var year = prompt("请输入您的出生年份");
var month = prompt("请输入您的出生月份");
var date = prompt("请输入您的出生日期");
//*已删除的部分* return year,month,date; 返回时,不能用这样返回多个值。
return cal(year,month,date);
//**后添加的**
}
/**因为有inp(); 开始调用函数方法 function inp(){...}
* 函数内的数据流动经过:
* 1. function inp(){
* ....
* return cal(year,month,date);
* }
* return 的返回效果先不执行,因为有cal(year,month,date),
* 开始调用函数方法 function cal(year,month,date){...}
* *****************************************************
* 补充说明:
* 【cal(year,month,date);】的参数和【function cal(year,month,date){}】的参数关系
* ①.function cal(num1,num2,num3){...} 方法的参数
* 相当于在函数内声明新变量。
* var num1;
* var num2;
* var num3;
* ②.cal(year,month,date); 函数的参数
* 相当于给①中声明的参数赋值。
* var num1 = year;
* var num2 = month;
* var num3 = date;
* ①和②参数名字一样也没有影响,名字相同时 函数内的变量 和函数外的变量 都是单独的个体不会影响。
* 函数方法内的变量在退出 函数方法后会销毁。
* ************************************************
* 2.function cal(year,month,date){
* ....
* ....
* return day;
* }
* day计算完后 返回 day的【值】到上一级函数 inp(){... return ...}
* 例如:day在cal()函数中最后的值为155
* return cal(year,month,date); --->因为函数call()返回day ----> 变成 return 155;
* 注意 返回的不是变量 只是值而已!!!
* 3.inp(){
* ....
* ....
* return 155;
* }
*
* 4.
* inp(); 等于 155; 返回的值 相对于没有身体的灵魂。
* 你想看到实际结果需要套上一个输出方法。
* document.write(inp()); 155 数据类型为 计算时的类型。
*
*
*
**/
document.write(inp());
function cal(year,month,date) {
var formerdays = new Array();
formerdays[0] = 0;
formerdays[1] = 31;
formerdays[2] = 31 + 28;
formerdays[3] = 31 + 28 + 31;
formerdays[4] = 31 + 28 + 31 + 30;
formerdays[5] = 31 + 28 + 31 + 30 + 31;
formerdays[6] = 31 + 28 + 31 + 30 + 31 + 30;
formerdays[7] = 31 + 28 + 31 + 30 + 31 + 30 + 31;
formerdays[8] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
formerdays[9] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
formerdays[10] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
formerdays[11] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
var yearInt = parseInt(year);
var monthInt = parseInt(month);
var dateInt = parseInt(date);
if (isNaN(yearInt) || isNaN(monthInt) || isNaN(dateInt)) {
alert("请输入正确数字");
return;
} else {
var day = formerdays[Number(monthInt) - 1] + Number(dateInt);//monthInt和dateInt已经是Number类型的。不需要Number()
if (yearInt % 4 == 0 && monthInt != 1 && monthInt != 2) {//没明白你想表达的意思 月份,为什么月份不等于 1和 2时 ,总天数还要加1天?
day++;
}
return day;
}
}我之前尝试这样封装,还是报错不是很明确如何修正错误
function inp(){
var year = prompt("请输入您的出生年份");
var month = prompt("请输入您的出生月份");
var date = prompt("请输入您的出生日期");
return year,month,date;
}
inp();
function cal(year,month,date) {
var formerdays = new Array();
formerdays[0] = 0;
formerdays[1] = 31;
formerdays[2] = 31 + 28;
formerdays[3] = 31 + 28 + 31;
formerdays[4] = 31 + 28 + 31 + 30;
formerdays[5] = 31 + 28 + 31 + 30 + 31;
formerdays[6] = 31 + 28 + 31 + 30 + 31 + 30;
formerdays[7] = 31 + 28 + 31 + 30 + 31 + 30 + 31;
formerdays[8] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
formerdays[9] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
formerdays[10] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
formerdays[11] = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
var yearInt = parseInt(year);
var monthInt = parseInt(month);
var dateInt = parseInt(date);
if (isNaN(yearInt) || isNaN(monthInt) || isNaN(dateInt)) {
alert("请输入正确数字");
return;
} else {
var day = formerdays[Number(monthInt) - 1] + Number(dateInt);
if (yearInt % 4 == 0 && monthInt != 1 && monthInt != 2) {
day++;
}
return day;
}
}
document.write("您的生日在" + year + "的第" + cal(year, month, date) + "天");
- 参与学习 人
- 提交作业 11218 份
- 解答问题 36712 个
从一个不会编程的小白到一个老司机是需要过程的,首先得入门,学习基础知识,然后才能进阶,最后再到精通,本专题是你走进前端世界的不二选择!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星