应用要求: 通过用户输入出生年月日/或者姓名,来判断用户的星座/人品,给出一个结论 提示: 将功能开发成为一个独立的js文件,并通过html页面加载,运行。用IDEA写出完整的代码
时间: 2023-05-23 12:06:34 浏览: 184
以下是应用要求的代码:
// 获取用户输入的信息
let birthDate = prompt("请输入您的出生年月日,格式为yyyy-mm-dd", "");
let name = prompt("请输入您的姓名", "");
// 解析出生年月日
let year = parseInt(birthDate.substr(0, 4));
let month = parseInt(birthDate.substr(5, 2));
let day = parseInt(birthDate.substr(8, 2));
// 计算星座
let constellations = [
{ name: "白羊座", startMonth: 3, startDay: 21, endMonth: 4, endDay: 19 },
{ name: "金牛座", startMonth: 4, startDay: 20, endMonth: 5, endDay: 20 },
{ name: "双子座", startMonth: 5, startDay: 21, endMonth: 6, endDay: 21 },
{ name: "巨蟹座", startMonth: 6, startDay: 22, endMonth: 7, endDay: 22 },
{ name: "狮子座", startMonth: 7, startDay: 23, endMonth: 8, endDay: 22 },
{ name: "处女座", startMonth: 8, startDay: 23, endMonth: 9, endDay: 22 },
{ name: "天秤座", startMonth: 9, startDay: 23, endMonth: 10, endDay: 23 },
{ name: "天蝎座", startMonth: 10, startDay: 24, endMonth: 11, endDay: 21 },
{ name: "射手座", startMonth: 11, startDay: 22, endMonth: 12, endDay: 21 },
{ name: "摩羯座", startMonth: 12, startDay: 22, endMonth: 1, endDay: 19 },
{ name: "水瓶座", startMonth: 1, startDay: 20, endMonth: 2, endDay: 18 },
{ name: "双鱼座", startMonth: 2, startDay: 19, endMonth: 3, endDay: 20 }
];
let constellation = "";
for(let i = 0; i < 12; i++) {
if((month == constellations[i].startMonth && day >= constellations[i].startDay) || (month == constellations[i].endMonth && day <= constellations[i].endDay)) {
constellation = constellations[i].name;
break;
}
}
// 计算人品
let score = 0;
for(let i = 0; i < name.length; i++) {
score += name.charCodeAt(i);
}
score = score % 100;
// 输出结论
alert(`您的星座是${constellation},您的人品得分是${score}分。`);
将上述代码保存为一个名为"astrology.js"的js文件,并在html页面中引用该文件。例如:
<!DOCTYPE html>
<html>
<head>
<title>星座和人品计算器</title>
<meta charset="utf-8">
<script src="astrology.js"></script>
</head>
<body>
<h1>星座和人品计算器</h1>
<button onclick="calculate()">开始计算</button>
<script>
function calculate() {
// 调用astrology.js中的代码
}
</script>
</body>
</html>
阅读全文