weixin_41244451 2024-06-13 15:17 采纳率: 93.9%
浏览 1
已结题

怎样能即保存之前的所有题,又能更新错题的结果。请在原有函数中修改,并注释修改的代码放在哪个位置

运算页yunsuan.html:

省略............
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('请听题');
    }
}

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


请在原有函数中修改,并注释修改的代码放在哪个位置。
  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-13 15:17
    关注

    引自免费微信小程序:皆我百晓生

    要实现题目描述的功能,我们需要确保在用户重做题目时,原始的成绩列表页(chengji.html)能够更新用户的新尝试结果。为了达到这个目的,我们可以在yunsuan.html中更新questions数组中的相关条目,并在每次完成题目后存储更新后的数组到localStorage中。同时,在chengji.html加载时,读取localStorage中的数据,这样就可以显示最新的结果。

    以下是具体的修改步骤:

    修改yunsuan.html中的checkAnswer()函数

    我们需要在用户回答完题目后,不仅更新questions数组,还要确保更新的信息能够持久化。因此,我们会在checkAnswer()函数中增加对localStorage的更新操作。

    // 在checkAnswer()函数中添加对localStorage的更新
    function checkAnswer() {
        // ... 省略其他代码 ...
        if (userAnswer === questions[currentQuestionIndex].answer) {
            alert('正确!');
        } else {
            alert('错误,正确答案是: ' + questions[currentQuestionIndex].answer);
        }
        // 更新用户答案和是否正确的状态
        questions[currentQuestionIndex].userAnswer = userAnswer;
        questions[currentQuestionIndex].isCorrect = userAnswer === questions[currentQuestionIndex].answer;
    
        // 更新localStorage
        localStorage.setItem('questionsData', JSON.stringify(questions));
    
        // ... 省略其他代码 ...
    }
    

    修改chengji.html中的数据加载逻辑

    chengji.html加载时,我们需要从localStorage中读取最新的数据,并且根据这些数据更新成绩列表。

    // 在window.onload函数中修改数据加载逻辑
    window.onload = function() {
        // 从localStorage中读取最新的数据
        const questionsData = JSON.parse(localStorage.getItem('questionsData'));
        // ... 其他代码保持不变 ...
    
        // 使用最新的数据填充表格
        questionsData.forEach((question, index) => {
            // ... 填充表格的代码保持不变 ...
        });
    };
    

    通过上述修改,当用户在yunsuan.html中重做题目并提交新答案时,questions数组会被更新,同时更新的信息会被存储到localStorage中。当用户返回到chengji.html时,页面会加载localStorage中的最新数据,从而显示最新的成绩列表。

    这样,即使用户多次重做题目,成绩列表页也能实时反映用户的最新尝试结果,满足了题目要求的即时保存和更新功能。

    评论 编辑记录

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓
  • ¥15 有人会用py或者r画这种图吗