weixin_41244451 2024-06-13 13:47 采纳率: 92.9%
浏览 3
已结题

怎样能即保存之前的所有题,又能更新错题的结果。多展示了一个generateQuestion(numbers)函数。请在原有函数中修改

运算页:

省略............
function generateQuestion(numbers) {
    let result = 0;
    const operators = ['+', '-'];
    let question = '';
    let lastOperator = '+'; // 初始操作符为加号
    let lastNumber = null; // 初始化上一个数字为null
    let buttonsDisabled = true; // 按钮初始状态为不可点击
    
    if(type==7)
    {
        if(bishu==2)
        {
            bishu=3;
        }
    }
    if(numbers!='')//重做
    {      
        let parts = numbers.split(' ');
        let denghao='';
 
        for (let i = 0; i < parts.length; i++) {
            let part = parts[i];
            let operator = part[0];//获取运算符
            let number=parseInt(part.slice(1), 10);;//获取数字
            
            if (i > 0) {
                result += operator === '+' ? number : -number;
                question += ' ' + operator + number;
            } else {
                result = number;
                question = operator + number;
            }
            
            // 每隔一定时间显示数字和运算符
            setTimeout(() => {
                if(i>0)
                {
                    number=operator+number;
                }
                if(i===parts.length-1)
                {
                    denghao='=';
                }
                displayQuestion(number, '', denghao);
            }, (i + 1) * shijian * 1000);
        }
    }
if(xingshi==1 || xingshi==2)
    {
        setTimeout(() => {
            document.getElementById('question').textContent = '请答题';
            isQuestionDisplayed = true; // 在显示“请答题”后将标志设为true
        }, shijian*bishu*1000+1000);
    }
    else
    {
        let yanshi=shijian*1000+1000+1000*1.5*bishu-300*bishu*yusu/4;
        setTimeout(() => {
            document.getElementById('question').textContent = '请答题';
            isQuestionDisplayed = true; // 在显示“请答题”后将标志设为true
        }, yanshi);
    }
    questions.push({ question: question, answer: result });
}


function checkAnswer() {
    if($.trim($("input[name=answerInput]").val()) == "")
    {
        $("input[name=answerInput]").focus();
        return false;
    }
    document.getElementById('question1').textContent='';
    isQuestionDisplayed = false;//每一题重新禁用按纽
    const userAnswer = parseInt(document.getElementById('answerInput').value);
    if (userAnswer === questions[currentQuestionIndex].answer) {
        alert('正确!');
    } else {
        alert('错误,正确答案是: ' + questions[currentQuestionIndex].answer);
    }
    document.getElementById('answerInput').value = '';
    $("input[name=answerInput]").focus();
    
    if (userAnswer !== undefined) { // 确保 userAnswer 已被定义
        questions[currentQuestionIndex].userAnswer = userAnswer; // 添加这一行
    }
    if (userAnswer === questions[currentQuestionIndex].answer) {
        questions[currentQuestionIndex].isCorrect = true;
    } else {
        questions[currentQuestionIndex].isCorrect = false;
    }
    let endTime = new Date().getTime(); // 记录结束时间
    let totalTime = (endTime - startTime) / 1000; // 转换为秒
    localStorage.setItem('questionsData', JSON.stringify(questions));
    
    if(tiliang==1)
    {
        if (currentQuestionIndex < jidaoti) {//几道题
            currentQuestionIndex++;
            document.getElementById('question').textContent = '';
            generateQuestion(numbers);
        } else {
            if(numbers!='')
            {
                window.history.back();
            }
            else
            {
                window.location.href = "/oa/oa_zxsxl.php?act=jieguo&totalTime="+totalTime+"&type={$type}&type_text={$type_text}&xingshi={$xingshi}&xingshi_text={$xingshi_text}&tiliang={$tiliang}&zushu={$zushu}&bishu={$bishu}&shijian={$shijian}&yusu={$yusu}";
            }
        }
    }
    else
    {
        currentQuestionIndex++;
        document.getElementById('question').textContent = '';
        generateQuestion(numbers);
    }
    if(xingshi==1 || xingshi==2)
    {
        document.getElementById('question').textContent = '请看题';
    }
    else
    {
        document.getElementById('question').textContent = '请听题';
        speak1('请听题');
    }
}

成绩列表页:

html部分省略..............
<script>
window.onload = function() {
    const questionsData = JSON.parse(localStorage.getItem('questionsData'));
    const resultsTableBody = document.getElementById('resultsTableBody');
    let correctCount = 0;
    let wrongCount = 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++;
        }
    });
    // 更新段落文本
    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';
        }
        
        cell1.textContent = index + 1;
        cell2.textContent = question.question.slice(1);
        if (question.isCorrect) {} else {
            cell2.innerHTML+=' <button onclick="chongzuo(\'' + question.question + '\')" style="height: 30px; font-size: 15px;"> 重做 </button>';
        }
        // 根据是否正确显示答案
        cell3.textContent = question.userAnswer;
        cell4.textContent = question.answer;
        cell5.textContent = question.isCorrect ? '√' : '×';
    });
};
 
function chongzuo(numbers) {
    // 将题目信息编码并附加到URL
    var encodedNumbers = encodeURIComponent(numbers); // 对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;
}
</script>

全部运算完以后进入到成绩列表页,这时会把所有做过的题全部列出来,然后算错的题后面有个重做按纽,点击重做后又进入到运算页,把错的题做一遍,做完这道错题后又进入到成绩列表,这时成绩列表只显示这一道错题,之前其它的题就没有显示了,于是我就做了一个判断,当是重做时,就window.history.back();返回到上一页,但是这样又有一个问题:这样返回去虽然保住了之前的其它所有题,但是我重做的最后结果没有更新。怎样能即保存之前的所有题,又能更新错题的结果。比如成绩列表页为:

 序号                      题目                     我的答案           正确答案          是否正确
    1                1+2+3【重做】                  3                        6                     ×
    2                 3+2+3【重做】                 5                        8                     ×
    3                 5-4+1                                2                        2  4                 3-2+3                                4                        4                     √
 
如果重做第一题后,如果答案还是不对,成绩列表页为:
 序号                      题目                     我的答案           正确答案          是否正确
    1                1+2+3【重做】                  5                        6                     ×
    2                 3+2+3【重做】                 5                        8                     ×
    3                 5-4+1                                2                        2  4                 3-2+3                                4                        4                     √
 
如果重做第一题后,如果答案对了,成绩列表页为:
 序号                      题目                     我的答案           正确答案          是否正确
    1                1+2+3                                 6                        6  2                 3+2+3【重做】                 5                        8                     ×
    3                 5-4+1                                2                        2  4                 3-2+3                                4                        4                     √
 
接着做第二题,如果答案不对,成绩列表为:
 序号                      题目                     我的答案           正确答案          是否正确
    1                1+2+3                                 6                        6  2                 3+2+3【重做】                 7                        8                     ×
    3                 5-4+1                                2                        2  4                 3-2+3                                4                        4                     √
 
接着做第二题,如果答案对了,成绩列表为:
 序号                      题目                     我的答案           正确答案          是否正确
    1                1+2+3                                 6                        6  2                 3+2+3                                8                        8  3                 5-4+1                                2                        2  4                 3-2+3                                4                        4 

多展示了一个generateQuestion(numbers)函数。请在原有函数中修改。

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-13 13:47
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    要实现题目重做功能并更新错题结果,同时保存所有题目数据,需要对generateQuestioncheckAnswer函数进行一些修改。以下是具体的修改建议:

    修改generateQuestion函数

    1. 获取URL参数中的题目数据:如果存在numbers参数,说明用户正在重做题目。需要从URL中解析出题目数据,并使用这些数据生成题目。

    2. 更新questions数组:在重做题目时,应该找到对应的题目索引,并更新该题目的数据,而不是简单地向数组中添加新题目。

    修改后的generateQuestion函数可能如下所示:

    function generateQuestion(numbers) {
        // 如果numbers不为空,说明是重做题目
        if (numbers) {
            let parts = numbers.split(' ');
            let question = '';
            let result = 0;
    
            // 重构题目字符串和计算结果
            for (let i = 0; i < parts.length; i++) {
                let part = parts[i];
                let operator = part[0];
                let number = parseInt(part.slice(1), 10);
                
                if (i > 0) {
                    result += (operator === '+' ? 1 : -1) * number; // 根据运算符更新结果
                    question += ' ' + operator + number;
                } else {
                    result = number;
                    question = operator + number;
                }
            }
    
            // 找到对应的题目索引,这里假设numbers是题目的唯一标识符
            let questionIndex = questions.findIndex(q => q.question === question);
            if (questionIndex !== -1) {
                // 更新已存在的题目数据
                questions[questionIndex] = { ...questions[questionIndex], question: question, answer: result };
            } else {
                // 如果没有找到,可能是新题目,添加到数组中
                questions.push({ question: question, answer: result, isCorrect: false, userAnswer: null });
            }
        } else {
            // 正常生成新题目的逻辑
            // ...
        }
    }
    

    修改checkAnswer函数

    1. 更新题目的答题信息:在用户提交答案后,需要更新当前题目的用户答案和正确性状态。

    2. 存储更新后的题目数据:将更新后的题目数组存储到localStorage

    修改后的checkAnswer函数可能如下所示:

    function checkAnswer() {
        // ... 省略现有代码 ...
    
        const userAnswer = parseInt(document.getElementById('answerInput').value, 10);
        const currentQuestion = questions[currentQuestionIndex];
    
        // 更新当前题目的用户答案和正确性状态
        currentQuestion.userAnswer = userAnswer;
        currentQuestion.isCorrect = userAnswer === currentQuestion.answer;
    
        // 如果用户答案正确,移除"重做"标识
        if (currentQuestion.isCorrect) {
            currentQuestion.question = currentQuestion.question.replace('[重做]', '');
        }
    
        // 存储更新后的题目数据
        localStorage.setItem('questionsData', JSON.stringify(questions));
    
        // ... 省略现有代码 ...
    }
    

    成绩列表页的JavaScript代码

    在成绩列表页,当用户点击“重做”按钮时,需要从URL参数中获取题目的原始数据,并更新questions数组。这里可以使用chongzuo函数来实现:

    function chongzuo(numbers) {
        // 解码numbers参数
        const decodedNumbers = decodeURIComponent(numbers);
        // 重新生成题目
        generateQuestion(decodedNumbers);
        // 存储当前的题目索引,以便重做后返回到正确的题目
        sessionStorage.setItem('currentQuestionIndex', currentQuestionIndex);
        // 跳转到运算页
        window.location.href = '/oa/oa_zxsxl.php?...&numbers=' + encodeURIComponent(decodedNumbers);
    }
    

    请注意,这里的代码示例仅供参考,具体实现可能需要根据你的应用程序逻辑进行调整。此外,我没有提供具体的参考资料地址,因为这些修改建议是基于你提供的代码和需求进行的定制化解决方案。如果你需要进一步的帮助或者有其他问题,请随时提问。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月13日
  • 已采纳回答 6月13日
  • 创建了问题 6月13日