Yağız.
Hectopat
- Katılım
- 29 Ocak 2020
- Mesajlar
- 86
- Çözümler
- 2
Daha fazla
- Cinsiyet
- Erkek
Merhaba iyi sosyaller. Yazdığım kod JSON dosyasından rastgele bir soru seçmeyi sağlıyor fakat, seçilen soruyu tekrardan seçince program amacından çıkıyor ve çok büyük bir karışıklık oluyor. Bu sorunu çözmek için (daha öncesinden seçilmiş olan bir soruyu tekrardan seçmemek) kodu nasıl düzenlemeliyim?
JavaScript:
_this.insertQuestion = function (data) {
var questionIndex = Math.floor(Math.random() * data.results.length); // Rastgele bir soru
var question = data.results[questionIndex];
var incorrectAnswers = question.incorrect_answers;
var correctAnswer = _this.htmlDecode(question.correct_answer);
var allAnswers = _this.shuffle(
incorrectAnswers.concat(correctAnswer)
);
var idxCorrAns = allAnswers.indexOf(correctAnswer);
_this.setState({
question: question.question,
correctAnswer: correctAnswer,
idxCorrAns: idxCorrAns,
canAnswer: [true, true, true, true],
allAnswers: allAnswers,
loading: false,
});
};
_this.getQuestion = function () {
if (_this.state.name === "1")
fetch("./deneme1.json")
.then(function (data) {
if (data.ok) {
return data.json();
} else {
throw new Error("Veriyi alırken sorun yaşandı.");
}
})
.then(function (data) {
_this.insertQuestion(data);
})
.catch(function (error) {
console.log(error);
});
};
Son düzenleyen: Moderatör: