英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论