React Native在问答应用中出现重复问题的问题

huangapple go评论71阅读模式
英文:

React-native problem with repeating questions in quiz app

问题

我是React Native的初学者,也许我的问题很愚蠢,但我正在构建一个问答应用程序,从本地数据库(data.js)获取数据。随机问题已经正确加载,但我不希望它们重复出现。我只想加载问题(这部分已完成),然后防止它再次加载。

我尝试检查如果data.length > 0,然后生成随机问题。然后我从data中删除这个问题,以防止它重复出现,一切都正常工作,但是当data.length = 0时,它什么也不做,因为没有剩余的问题了。
我尝试使用.concat()方法添加newData,但是当我从data中删除问题时,它也会从newData中删除问题。我该如何修复它并重新加载数据,以便您可以随意进行问答?

请帮帮我,我无法处理这个问题。

这是我的代码片段:

import { data } from './data.js';

const newData = require('./data.js');

const getQuestions = () => {
    if (data.length > 0) {
        setSelectedOptions({});
        setShowResult(false);

        const currentQuestion = data.sort(() => 0.5 - Math.random());
        const slicedQuestion = currentQuestion.slice(0, 1);

        if (slicedQuestion[0].category === category) {
            setQuestions(slicedQuestion);
        } else {
            getQuestions();
        }
    } else {
        data.length = 0;
        data.concat(newData.data);
    }
}

const handleNext = () => {
    questions.forEach((questions, index) => {
        if (selectedOptions[index] === questions.correctOption) {
            const indexElement = data.indexOf(questions);
            data.splice(indexElement, 1);
            console.log(data.length);
            navigation.navigate('Correct Site');
            getQuestions();
        }
    });
}

希望对你有帮助!

英文:

I'm a beginner to react-native and maybe my question is stupid, but I'm building a quiz app, that takes data from local database - data.js. Random questions are loading properly, but I don't want them to repeat. I just want to load question(this part is done) and after that prevent it from loading again.

I tried to check if data.length > 0 then I generate random question. After that I remove this question from data to prevent it from repeating and everything works fine but after reaching data.length = 0 it does nothing because there are no questions left.
I tried to append newData using .concat() but when I remove question from data it also removes question from newDeta. How can I fix it and load data again so that you can take quiz as long as u want?

Please help me I cannot deal with it.

Here's fragment of my code:


import { data } from './data.js'

const newData = require('./data.js')

    const getQuestions = () => {
        if(data.length > 0){
            setSelectedOptions({});
            setShowResult(false);

            const currentQuestion = data.sort(() => 0.5 - Math.random())
            const slicedQuestion = currentQuestion.slice(0, 1)
                
            if(slicedQuestion[0].category === category){
                setQuestions(slicedQuestion) 
            }
            else{
                getQuestions();
            }
        }
        else{
            data.length = 0
            data.concat(newData.data)
        }

    }

    const handleNext = () => {
        questions.forEach((questions, index) => {
            if(selectedOptions[index] === questions.correctOption){
                const indexElement = data.indexOf(questions)
                data.splice(indexElement, 1)
                console.log(data.length)
                navigation.navigate('Correct Site');
                getQuestions();
            }

答案1

得分: 0

现在我知道我犯了一个错误。我通过简单地将我的本地数据库复制到一个新的数组中来修复它,然后从这个复制的数组中加载问题。之后,每次加载问题时,我都会从复制的数组中删除该问题。

英文:

Now I know where I made a mistake. I fixed it by simply creating a copy of my local database to a new array and then loading questions from this copied array. After that I every time I load question I delete question from copied array.

huangapple
  • 本文由 发表于 2023年8月9日 02:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定