【娱乐项目】竖式算术器
// 正确和错误的答题次数初始化
var correctCount = 0;
var wrongCount = 30;
// 初始最大随机数范围为20
var randomNum = 20;
// 用于保存历史记录和错题记录
var previousEquations = []; // 所有的算式
var correctEquations = []; // 正确的算式
var wrongEquations = []; // 错误的算式
var showWrongRecords = false; // 当前是否展示错题记录
// 记录开始时间,用于计时
var startTime = new Date().getTime();
var timerElement = document.getElementById('timer');
const edit = document.querySelector('#edit');
const input = document.querySelector('.title input');
// 点击“修改”按钮,允许用户修改最大随机数
edit.addEventListener('click', () => {
if (edit.textContent == '修改') {
input.readOnly = false;
input.focus();
edit.textContent = '确定'
} else {
input.readOnly = true;
input.blur();
const value = input.value;
edit.textContent = '修改'
input.style.border = 'none';
randomNum = value; // 修改最大随机数
}
});
// 更新计时器
function updateElapsedTime() {
var currentTime = new Date().getTime();
var elapsedTime = Math.floor((currentTime - startTime) / 1000); // 换算为秒
if (elapsedTime >= 60) {
var minutes = Math.floor(elapsedTime / 60);
var seconds = elapsedTime % 60;
timerElement.textContent = '耗时:' + minutes + '分' + seconds + '秒';
} else {
timerElement.textContent = '耗时:' + elapsedTime + '秒';
}
}
// 每秒更新一次计时器
setInterval(updateElapsedTime, 1000);
// 生成1到randomNum之间的随机数
function generateRandomNumber() {
return Math.floor(Math.random() * randomNum) + 1; // 生成1到randomNum之间的随机数
}
// 随机生成加法或减法
function generateRandomOperation() {
return Math.random() < 0.5 ? '+' : '-'; // 以50%的概率生成加减法
}
// 生成随机算式
function generateRandomEquation() {
var number1 = generateRandomNumber();
var number2 = generateRandomNumber();
var operation = generateRandomOperation();
if (operation === '+' && number1 + number2 > randomNum) {
operation = '-'; // 如果加法结果超过最大值,则改为减法
}
if (operation === '-' && number1 < number2) {
var temp = number1;
number1 = number2;
number2 = temp; // 如果减法第一个数小于第二个数,交换
}
let res = number1 + ' ' + operation + ' ' + number2;
let show = '<div class="tree"><p>' + number1 + '</p><p class="fuhao">' + operation + '</p><p>' + number2 + '</p></div>';
let obj = {
res,
show
};
return obj;
}
// 检查用户输入的答案
function checkAnswer(event) {
if (event.keyCode === 13) { // 检测是否按下回车键
var userInput = document.getElementById('answer').value;
var equation = document.getElementById('equation').textContent;
var result = eval(equation); // 使用eval计算算式的结果
if (userInput == "") {
return;
}
var isCorrect = parseInt(userInput) === result;
// 判断答案是否正确
if (isCorrect) {
correctCount++;
previousEquations.push('<span class="equation-item correct">' + equation + ' = ' + userInput + ' √</span>');
correctEquations.push('<span class="equation-item correct">' + equation + ' = ' + userInput + ' √</span>');
document.getElementById('result').textContent = '回答正确!';
} else {
wrongCount++;
previousEquations.push('<span class="equation-item wrong">' + equation + ' = ' + userInput + ' ×</span>');
let equationItem = '<span class="equation-item wrong">' + equation + ' = </span>';
if (!wrongEquations.includes(equationItem)) {
wrongEquations.push(equationItem); // 错题不重复
}
document.getElementById('result').textContent = '回答错误!';
}
document.getElementById('correctCount').textContent = '回答正确次数:' + correctCount;
document.getElementById('wrongCount').textContent = '回答错误次数:' + wrongCount;
document.getElementById('previousEquations').innerHTML = previousEquations.join('');
// 答对了清空输入框并生成新题
if (isCorrect) {
generateNewEquation();
}
document.getElementById('answer').value = ''; // 清空输入框
document.getElementById('answer').focus(); // 聚焦输入框
showWrongRecords = false;
showHistory();
}
}
// 生成新的随机算式
function generateNewEquation() {
document.getElementById('answer').value = ''; // 清空输入框
document.getElementById('result').textContent = ''; // 清空结果显示
var equation;
var show;
var result;
// 确保生成的算式结果是非负数
do {
let fun = generateRandomEquation();
equation = fun.res;
show = fun.show;
result = eval(equation);
} while (result < 0); // 重新生成随机算式,直到结果不是负数为止
document.getElementById('equation').textContent = equation;
document.getElementById('equation2').textContent = equation + " = ";
document.getElementById('showRes').innerHTML = show;
}
// 显示历史记录或错题记录
function showHistory() {
var historyTitle = document.getElementById('historyTitle');
var previousBox = document.getElementById('previousEquations');
if (showWrongRecords) {
historyTitle.textContent = '错题记录';
previousBox.innerHTML = wrongEquations.join('');
} else {
historyTitle.textContent = '历史记录';
previousBox.innerHTML = previousEquations.join('');
}
}
// 切换显示历史记录或错题记录
function toggleRecords() {
showWrongRecords = !showWrongRecords;
showHistory();
}
document.addEventListener('keypress', checkAnswer); // 监听键盘按键事件
generateNewEquation(); // 生成第一道题目
document.getElementById('answer').focus(); // 页面加载后将焦点聚焦到输入框
var toggleRecordsBtn = document.getElementById('toggleRecordsBtn');
toggleRecordsBtn.addEventListener('click', toggleRecords); // 点击切换记录按钮