户输入出生年月日/或者姓名,来判断用户的星座/人品,给出一个结论 将功能开发成为一个独立的js文件,并通过html页面加载,运行。
时间: 2023-05-23 07:06:45 浏览: 173
python 开发根据出生日期判断星座demo源码+注解清晰一看就懂.zip
// star.js
function getConstellation(month, day) {
const constellations = [
"魔羯座",
"水瓶座",
"双鱼座",
"白羊座",
"金牛座",
"双子座",
"巨蟹座",
"狮子座",
"处女座",
"天秤座",
"天蝎座",
"射手座",
"魔羯座",
];
const dates = [20, 19, 21, 20, 21, 21, 22, 23, 23, 23, 22, 22];
const index = month - 1;
if (day < dates[index]) {
return constellations[index];
}
return constellations[index + 1];
}
function getLucky(name) {
let sum = 0;
for (let i = 0; i < name.length; i++) {
sum += name.charCodeAt(i);
}
return sum % 100;
}
// index.html
<!DOCTYPE html>
<html>
<head>
<title>星座和人品测试</title>
<script src="star.js"></script>
</head>
<body>
<h1>星座测试</h1>
<label>请输入出生年月日:</label>
<input type="text" id="birth-date" />
<button onclick="testConstellation()">测试</button>
<p id="constellation-result"></p>
<h1>人品测试</h1>
<label>请输入姓名:</label>
<input type="text" id="name" />
<button onclick="testLucky()">测试</button>
<p id="lucky-result"></p>
<script>
function testConstellation() {
const birthDate = document.getElementById("birth-date").value;
const [year, month, day] = birthDate.split("-");
const constellation = getConstellation(parseInt(month), parseInt(day));
document.getElementById(
"constellation-result"
).innerHTML = `你的星座是 ${constellation}`;
}
function testLucky() {
const name = document.getElementById("name").value;
const lucky = getLucky(name);
document.getElementById(
"lucky-result"
).innerHTML = `你的人品指数是 ${lucky}`;
}
</script>
</body>
</html>
阅读全文