运算页:
function generateQuestion(numbers) {
let result = 0;
const operators = ['+', '-'];
let question = '';
let lastOperator = '+'; // 初始操作符为加号
let lastNumber = null; // 初始化上一个数字为null
let buttonsDisabled = true; // 按钮初始状态为不可点击
if(type<1000)//加减法
{
for (let i = 0; i < 2; i++)
{
let number, operator;
let validNumber = false;
let result_onesDigit=onesDigit(result);
let result_tensDigit = tensDigit(result);
let result_hundredDigit = hundredDigit(result);
// 循环直到找到一个合适的数字
do {
if(type==1)//1-4直加直减
{
number = Math.floor(Math.random() * 4) + 1; // 1-4的随机数
operator = operators[Math.floor(Math.random() * 2)];
// 检查当前操作符和数字是否会导致结果超出范围
if (operator === '+') {
validNumber = result + number <= 4;
}
else
{
validNumber = result - number >= 0;
}
}
} while (!validNumber);
lastOperator = operator; // 更新上一个操作符
lastNumber = number; // 更新上一个数字
setTimeout(() =>
{
displayQuestion(number,'','');
}, (i + 1) * shijian*1000);
result += operator === '+' ? number : -number;
question += (i > 0 ? ' ' : '') + operator + number;
}
}
else//乘除法
{
let number1, operator, number2;
let validNumber = false;
if(type==1000)//2的乘法
{
number1 = Math.floor(Math.random() * 90) + 10; // 两位数
number2 = 2;
operator = '*';
result = number1 * number2;
}
lastOperator = operator; // 更新上一个操作符
question = `${number1} ${operator} ${number2}`; // 同时显示操作符和数字
setTimeout(() => {
document.getElementById('question').textContent = number1+'×'+number2;
}, shijian*1000);
}
// 用于存储问题
questions.push({ question: question, answer: result });
}
结果页:
window.onload = function() {
const questionsData = JSON.parse(localStorage.getItem('questionsData'));
const resultsTableBody = document.getElementById('resultsTableBody');
let timulibiao='';
let cz='{$quanbu_z}';
let correctCount = 0;
let wrongCount = 0;
let dui_count = 0;
let cuo_count = 0;
// 对questionsData进行排序,错题在前,对题在后
questionsData.sort((a, b) => {
return a.isCorrect === b.isCorrect ? 0 : a.isCorrect ? 1 : -1;
});
// 计算对错题数
questionsData.forEach(question => {
if (question.isCorrect) {
correctCount++;
} else {
wrongCount++;
}
});
if(cz!='')
{
dui_count = parseInt("{$dui_count}");
cuo_count = parseInt("{$cuo_count}");
correctCount += dui_count;
wrongCount += cuo_count;
}
// 更新段落文本
const summaryElement1 = document.getElementById('summary1');
const summaryElement2 = document.getElementById('summary2');
const summaryElement3 = document.getElementById('summary3');
const summaryElement4 = document.getElementById('summary4');//准确率
let zts=correctCount+wrongCount;//总题数
let zql=correctCount/zts*100;
summaryElement1.textContent = `对题数:${correctCount}`;
summaryElement2.textContent = `错题数:${wrongCount}`;
summaryElement3.textContent = `总题数:${zts}`;
summaryElement4.textContent = `准确率:${zql.toFixed(2)}%`;
questionsData.forEach((question, index) => {
const row = resultsTableBody.insertRow();
const cell1 = row.insertCell(0);
const cell2 = row.insertCell(1);
const cell3 = row.insertCell(2);
const cell4 = row.insertCell(3);
const cell5 = row.insertCell(4);
// 添加class到每个cell
cell1.className = "biankuan2";
cell2.className = "biankuan3";
cell3.className = "biankuan3";
cell4.className = "biankuan3";
cell5.className = "biankuan3";
cell1.height = "40";
if (question.isCorrect) {} else {
cell1.style = 'color: crimson';
cell2.style = 'color: crimson';
cell3.style = 'color: crimson';
cell4.style = 'color: crimson';
cell5.style = 'color: crimson';
}
let xuhao=index + 1;
let duicuo=question.isCorrect ? '√' : '×';
let quanbu=xuhao+'//'+question.question+'//'+question.userAnswer+'//'+question.answer+'//'+duicuo+'@';
cell1.textContent = xuhao;
cell2.textContent = question.question.slice(1);
if (question.isCorrect) {} else {
cell2.innerHTML+=' <button onclick="chongzuo(\'' + question.question + '\',\'' + quanbu + '\')" style="height: 30px; font-size: 15px;"> 重做 </button>';
}
// 根据是否正确显示答案
cell3.textContent = question.userAnswer;
cell4.textContent = question.answer;
cell5.textContent = duicuo;
timulibiao+=quanbu;
});
let lb='';
if(cz!='')
{
lb='@'+cz;
}
document.getElementById('timulibiao').value=timulibiao.slice(0, -1)+lb;
};
function chongzuo(numbers,index) {
let timulibiao1 = document.getElementById('timulibiao').value;
timulibiao=timulibiao1.replace(index, "");
// 将题目信息编码并附加到URL
var encodedNumbers = encodeURIComponent(numbers); // 对numbers进行URL编码
var encodedTimulibiao = encodeURIComponent(timulibiao); // 对numbers进行URL编码
window.location.href = '/oa/oa_zxsxl.php?act=yunsuan&type={$type}&type_text={$type_text}&xingshi={$xingshi}&xingshi_text={$xingshi_text}&tiliang={$tiliang}&zushu={$zushu}&bishu={$bishu}&shijian={$shijian}&yusu={$yusu}&numbers=' + encodedNumbers+'&timulibiao=' + encodedTimulibiao;
}
运算页的questions.push({ question: question, answer: result })加减法的可以正常存储并传到结果页面去,但是乘除法的传到结果页上去以后,questionsData里的number1只显示了个位数,十位上的数没有显示。