应用要求: 通过用户输入出生年月日/或者姓名,来判断用户的星座/人品,给出一个结论 提示: 将功能开发成为一个独立的js文件,并通过html页面加载,运行。
时间: 2023-05-23 16:07:12 浏览: 111
A:
好的,我可以为您实现此功能。以下是代码示例:
//star.js
function getStar(sign_data) {
var star_signs = [
{ name: "水瓶座", start_date: "01-20", end_date: "02-18" },
{ name: "双鱼座", start_date: "02-19", end_date: "03-20" },
{ name: "白羊座", start_date: "03-21", end_date: "04-19" },
{ name: "金牛座", start_date: "04-20", end_date: "05-20" },
{ name: "双子座", start_date: "05-21", end_date: "06-21" },
{ name: "巨蟹座", start_date: "06-22", end_date: "07-22" },
{ name: "狮子座", start_date: "07-23", end_date: "08-22" },
{ name: "处女座", start_date: "08-23", end_date: "09-22" },
{ name: "天秤座", start_date: "09-23", end_date: "10-23" },
{ name: "天蝎座", start_date: "10-24", end_date: "11-22" },
{ name: "射手座", start_date: "11-23", end_date: "12-21" },
{ name: "摩羯座", start_date: "12-22", end_date: "01-19" }
];
var month_day = sign_data.split("-");
var user_birth = new Date();
user_birth.setMonth(parseInt(month_day[0]) - 1);
user_birth.setDate(parseInt(month_day[1]));
for (var i = 0; i < star_signs.length; i++) {
var start = new Date();
start.setMonth(parseInt(star_signs[i].start_date.split("-")[0]) - 1);
start.setDate(parseInt(star_signs[i].start_date.split("-")[1]));
var end = new Date();
end.setMonth(parseInt(star_signs[i].end_date.split("-")[0]) - 1);
end.setDate(parseInt(star_signs[i].end_date.split("-")[1]));
if (user_birth >= start && user_birth <= end) {
return star_signs[i].name;
}
}
}
function getGrade(name_data) {
var name = String(name_data);
var grade = 0;
for (var i = 0; i < name.length; i++) {
grade += name.charCodeAt(i);
}
grade %= 100;
return grade;
}
// index.html
<!DOCTYPE html>
<html>
<head>
<title>星座和人品测试</title>
</head>
<body>
<h1>星座和人品测试</h1>
<p>请输入您的出生年月日和姓名进行测试</p>
<form id="test-form">
<label for="birth-date">出生年月日:</label>
<input type="date" id="birth-date" name="birth-date">
<br />
<label for="name">姓名:</label>
<input type="text" id="name" name="name">
<br /><br />
<button type="submit">测试</button>
</form>
<p><strong>测试结果:</strong></p>
<div id="test-result"></div>
<script src="star.js"></script>
<script>
var form = document.getElementById("test-form");
var result = document.getElementById("test-result");
form.addEventListener("submit", function(e) {
e.preventDefault();
var birth_date = document.getElementById("birth-date").value;
var name = document.getElementById("name").value;
var star = getStar(birth_date);
var grade = getGrade(name);
result.innerHTML =
"您的星座是:" + star + "<br />" + "您的人品值为:" + grade;
});
</script>
</body>
</html>
以上代码将星座和人品测试功能独立存储在一个star.js文件中,通过index.html页面加载并运行。用户可输入出生年月日和姓名进行测试,并将结果显示在页面上。
阅读全文