主题
随机计算
- 随机 1-9 的计算公式, 不得重复,例如 1+2 仅能出现一次, 但是可以出现2+1
- 答对了自动进入下一题,答错了不继续下一题,依然保持这一题
- 重复N次后,统计总耗时,并且算出耗时最久的前5道题目
js
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
let count = 0;
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", (req, res) => {
if (isMaxrecordEnd()) {
res.send("<h1>您的机会已用完</h1>");
return;
}
const oldId = req.query.id;
let record;
if (oldId) {
record = getRecord(oldId);
} else {
record = addRecord();
}
console.log("CURRENT==>", JSON.stringify(record));
const { id, calc } = record;
totalLog();
res.send(`
<div style=" width:500px;margin:0 auto;">
<h1>随机计算</h1>
<form action="/submit" method="post">
<span style="font-size: 20px;">${calc} =</span>
<span>
<input type="number" name="sum" id="sum">
</span>
<span>
<button type="submit">提交</button>
</span>
<input type="hidden" name="id" value="${id}">
</form>
<br>
<a href="/restart">重新开始</a>
</div>
<script>
document.querySelector('#sum').focus()
</script>
`);
});
app.post("/submit", (req, res) => {
const { id, sum } = req.body;
if (isNaN(sum)) {
res.redirect(`/?id=${id}`);
return;
}
const record = getRecord(id);
if (+sum === +record.sum) {
ediRecord(+id);
if (++count >= 10) res.redirect("/history");
else res.redirect("/");
} else {
res.redirect(`/?id=${id}`);
}
});
app.get("/history", (req, res) => {
const top5 = getTop5();
res.send(`
<div style=" width:500px;margin:0 auto;">
<div>总耗时:${sumTime()}秒</div>
<div>答题耗时最多Top5</div>
<ul>
${top5
.map((item) => {
return `<li>题目:${item.calc} = ${item.sum} 耗时:${item.timeTaken} 秒</li>`;
})
.join(" ")}
</ul>
<a href="/restart">重新开始</a>
</div>
`);
});
app.get("/restart", (req, res) => {
count = 0;
init();
res.redirect("/");
});
app.listen(3000, () => {
console.log("server runing on http://127.0.0.1:3000");
});
const examedPool = [];
let uuid = 1;
const getRandom = () => Math.floor(Math.random() * 10);
function generateExam() {
const f = getRandom() || 1;
const s = getRandom() || 1;
const sum = f + s;
const execH = `${f} + ${s}`;
if (hasExam(execH)) return generateExam();
return {
execH,
sum,
};
}
function hasExam(e) {
return examedPool.findIndex((item) => item.calc === e) > -1;
}
function getRecord(id) {
return examedPool.find((item) => item.id === +id);
}
function addRecord() {
const { execH, sum } = generateExam();
const record = {
calc: execH,
id: uuid++,
sum,
timeStart: Date.now(),
timeTaken: 0,
};
examedPool.push(record);
return record;
}
function ediRecord(id) {
const record = getRecord(id);
if (!record) return;
record.timeEnd = Date.now();
record.timeTaken = Math.round((record.timeEnd - record.timeStart) / 1000);
}
function getTop5() {
const records = examedPool.sort((a, b) => b.timeTaken - a.timeTaken);
return records.slice(0, 5);
}
function sumTime() {
return examedPool.reduce((acc, record) => acc + (record.timeTaken || 0), 0);
}
function isMaxrecordEnd() {
return examedPool.length >= 81;
}
function init() {
examedPool.length = 0;
uuid = 1;
}
function totalLog() {
console.log("ALL===>", JSON.stringify(examedPool));
}