JavaScript ShowQuestions() Fonksiyonu Çalıştığında dogruSorular Dizisinin İçindeki Şeyler Siliniyor

Katılım
8 Ekim 2023
Mesajlar
256
Çözümler
2
Daha fazla  
Cinsiyet
Erkek
Merhabalar, kodumda bir hata alıyorum şöyle: Kullanıcı doğru yaptığında o soruyu checkAnswer fonksiyonunda dogruSorular adlı bir diziye kaydedeceğiz. Kaydetme işlemi başarılı oluyor fakat sonrasında ise showQuestions fonksiyonuna geçince dogruSorular dizisinin içi boş gözüküyor. Bunu nasıl engellerim sorun ne?

Tüm kodum:


JavaScript:
let questions = [
    {
        question: "<span>''İvedi''</span> <br> <br> Kelimesinin anlamı nedir?",
        options: ["Çabuk", "Aceleci", "Hızlı hareket edemeyen.", "Gereği yokken hızlı hareket etmek."],
        correct: "Çabuk"
    },
    {
        question: "<span>''Mahşer midillisi''</span> <br> <br> Kelimesinin anlamı nedir?",
        options: ["Ortalığı karıştıran kimse.", "Yaramaz, hayırsız kimseler için kullanılan bir söz.", "Hareketli, yerinde duramayan.", "Boş işlerle uğraşan kimse."],
        correct: "Ortalığı karıştıran kimse."
    },
    {
        question: "<span>''Gaye''</span> <br> <br> Kelimesinin anlamı nedir?",
        options: ["Amaç", "Gerekçe", "Şart", "Zorunluluk"],
        correct: "Amaç"
    }
];

let oyunBittiMi = false
let currentQuestionIndex = 0;
let userScore = { correct: 0, incorrect: 0 };
let questionFinishTime = 0;
let countdownTime = 60;
let atlananSorular = []
let yanlisSorular = []
let dogruSorular = []
let sureAsimiSorular = []


function updateCountdown() {
    const countdownElement = document.getElementById('sure');

    const minutes = Math.floor(countdownTime / 60);
    const seconds = countdownTime % 60;

    const formattedTime = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;

    countdownElement.innerText = formattedTime;

    if (countdownTime === 0) {
        timeIsUp();
    } else {
        countdownTime--;
    }
}

function startCountdown() {
    try{ stopCountdown(); } catch {}
    countdownInterval = setInterval(updateCountdown, 1000);
}

function stopCountdown() {
    clearInterval(countdownInterval);
}

function timeIsUp() {
    stopCountdown();
    questionsSkipped -= 1
    questionFinishTime += 1
    skipQuestion();
}


if (currentQuestionIndex === 0) {
    questions = shuffleArray(questions);
}


function shuffleOptions(options) {
    for (let i = options.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [options[i], options[j]] = [options[j], options[i]];
    }
}

function askQuestion() {
    if (currentQuestionIndex >= questions.length) {
        showResult();
        return;
    }

    const question = questions[currentQuestionIndex];
    shuffleOptions(question.options);

    document.querySelector(".soru").innerHTML = question.question;

    for (let i = 0; i < question.options.length; i++) {
        const optionElement = document.querySelectorAll(".secenekler")[i];
        optionElement.innerHTML = question.options[i];
        optionElement.classList.remove("correct", "incorrect");
    }
    countdownTime = 60;
    updateCountdown();
    startCountdown();
}
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}
let answerSelected = false;

function checkAnswer(option) {
    if (answerSelected || option.classList.contains('disabled')) return;

    answerSelected = true;

    const question = questions[currentQuestionIndex];

    if (option.innerHTML === question.correct) {
        dogruSorular.push(question.question)
        console.log("dogruSorular:", dogruSorular);
        option.classList.add("correct");
        userScore.correct++;
        earnCoins(25);

        const allOptions = document.querySelectorAll(".secenekler");
        allOptions.forEach((opt) => {
            if (opt !== option) {
                opt.classList.add("disabled");
            }
        });
    } else {
        option.classList.add("incorrect");
        userScore.incorrect++;
        if (coinCount >=25){
            earnCoins(-25)
        }
      
        let soru23 = question.question
        for (let i = 0; i < question.options.length; i++) {
            if (question.options[i] === question.correct) {
                document.querySelectorAll(".secenekler")[i].classList.add("correct");
            }
        }
    }

    const progressBar = document.querySelector('.progress-bar');
    const progressValue = Math.round((currentQuestionIndex / questions.length) * 100);

    if (currentQuestionIndex < questions.length - 1) {
        progressBar.style.width = `${progressValue}%`;
        progressBar.setAttribute('aria-valuenow', progressValue);

        setTimeout(() => {
            answerSelected = false;


            const allOptions = document.querySelectorAll(".secenekler");
            allOptions.forEach((opt) => {
                opt.classList.remove("disabled");
            });

            currentQuestionIndex++;
            askQuestion();

            if (option.classList.contains("correct")) {
                for (let i = 0; i < question.options.length; i++) {
                    if (question.options[i] === question.correct) {
                        document.querySelectorAll(".secenekler")[i].classList.remove("correct");
                    }
                }
            }
        }, 3000);
    } else {
        progressBar.style.width = '100%';
        progressBar.setAttribute('aria-valuenow', 100);

        document.querySelector(".sorucontainer").innerHTML = `<h3 class="yanlisGosterge">Yanlış Cevaplar: ${userScore.incorrect} <br> Doğru Cevaplar: ${userScore.correct} <br> Atlanan Sorular: ${questionsSkipped} <br> Zaman Dolan Sorular: ${questionFinishTime} </h3> <br> <button id="yapamadiginSorularaBak" class="bn632-hover bn24" onclick="yapilamayanaGonderme()">Yapamadığın Sorulara Bak</button>`;
        countdownTime =  60;
        oyunBittiMi = true
    }
}

function yapilamayanaGonderme(){
    window.location = "yapilamayansorular.html"
}


document.body.addEventListener('click', (event) => {
    if (event.target.classList.contains('secenekler') && !event.target.classList.contains('disabled')) {
        event.preventDefault();
    }
});

document.querySelectorAll(".secenekler").forEach((option, index) => {
    option.addEventListener("click", () => checkAnswer(option));
});

askQuestion();

document.getElementById('soruyuAtlaBtn').addEventListener('click', () => skipQuestion());

let questionsSkipped = 0;

function skipQuestion() {
    if (coinCount >=25){
        earnCoins(-25)
    }
    if (!answerSelected) {
        const question = questions[currentQuestionIndex];

        const correctOptionIndex = question.options.findIndex(opt => opt === question.correct);


        const correctOption = document.querySelectorAll(".secenekler")[correctOptionIndex];
        correctOption.classList.add("correct");

        const allOptions = document.querySelectorAll(".secenekler");
        allOptions.forEach(opt => {
            if (opt !== correctOption) {
                opt.classList.add("disabled");
            }
        });


        const skipButton = document.getElementById('soruyuAtlaBtn');
        skipButton.disabled = true;


        questionsSkipped++;

        setTimeout(() => {

            currentQuestionIndex++;
            askQuestion();
            answerSelected = false;

            updateProgressBar();

            correctOption.classList.remove("correct");
            allOptions.forEach(opt => opt.classList.remove("disabled"));
            skipButton.disabled = false;


            if (currentQuestionIndex === questions.length) {
                showResult();
            }
        }, 3000);
    }
}
function showResult() {
    document.querySelector(".sorucontainer").innerHTML = `<h3 class="yanlisGosterge">Yanlış Cevaplar: ${userScore.incorrect} <br> Doğru Cevaplar: ${userScore.correct} <br> Atlanan Sorular: ${questionsSkipped} <br> Zaman Dolan Sorular: ${questionFinishTime} </h3> <br> <button id="yapamadiginSorularaBak" class="bn632-hover bn24" onclick="yapilamayanaGonderme()">Yapamadığın Sorulara Bak</button>`;

}
function updateProgressBar() {
    const progressBar = document.querySelector('.progress-bar');
    const progressValue = Math.round((currentQuestionIndex / questions.length) * 100);
    progressBar.style.width = `${progressValue}%`;
    progressBar.setAttribute('aria-valuenow', progressValue);
}


function toggleSound() {
    var audioElement = document.getElementById('arkaFonMuzik');
  
    if (audioElement.paused) {
        audioElement.play();
    } else {
        audioElement.pause();
    }
    const soundButton = document.querySelector(".sesacmakapama");
  
    if (soundButton.classList.contains("sesacik")) {

        soundButton.innerHTML = '<i class="fa-solid fa-volume-off"></i>';
        soundButton.classList.remove("sesacik");
    } else {

        soundButton.innerHTML = '<i class="fa-solid fa-volume-high"></i>';
        soundButton.classList.add("sesacik");
    }
}

function cikisYapma(){

    if (oyunBittiMi == false){ 
        cikisonay = confirm("Çıkış yapılacak ve veri kaybolacak emin misiniz?")
        if (cikisonay == true){
            window.location = "games.html"
        }
    }
    else if (oyunBittiMi == true){
        window.location = "games.html"
    }
}

document.addEventListener("DOMContentLoaded", function () {
    showQuestions('correct'); // Varsayılan olarak doğru soruları göster
});

function showQuestions(type) {
    const soruListesiDiv = document.querySelector(".soru-listesi");
    soruListesiDiv.innerHTML = ""; // Her tıklamada önceki içeriği temizle

    for (let i = 0; i < questions.length; i++) {
        const soruOrnegi = document.createElement("div");
        dogruSorular.push("")
        console.log(dogruSorular)
        const soru = questions[i];
        let showQuestion = false;

        switch (type) {
            case 'correct':
                showQuestion = soru.answered && soru.correct;
                break;
            case 'incorrect':
                showQuestion = soru.answered && !soru.correct;
                break;
            case 'skipped':
                showQuestion = !soru.answered;
                break;
            case 'timeout':
                showQuestion = soru.answered && soru.timeout;
                break;
            default:
                break;
        }

        if (showQuestion) {
            const shuffledOptions = [...soru.options]; // Şıkları karıştırmak için kopyasını oluştur
            shuffleOptions(shuffledOptions); // Kopya şıkları karıştır

            const soruOrnegiDiv = document.createElement("div");
            soruOrnegiDiv.classList.add("soruornegidiv");

            if (soru.correct) {
                soruOrnegi.classList.add("correct");
            }
            if (soru.incorrect) {
                soruOrnegi.classList.add("incorrect");
            }

            soruOrnegi.classList.add("soruornegi");

            soruOrnegi.innerHTML = `
                <h2 class="h2">${i + 1}. Soru</h2>
                <p>${soru.question}</p>
                <div class="seceneklercontainer">
                    <div class="secenekcontainer1">${shuffledOptions.slice(0, 2).map((option, index) => `<button class="secenekler2 ${soru.correct === option ? 'correct' : (soru.selectedAnswer === option && !soru.correct ? 'incorrect' : '')}" onclick="return false;">${option}</button>`).join('')}</div>
                    <div class="secenekcontainer2">${shuffledOptions.slice(2).map((option, index) => `<button class="secenekler2 ${soru.correct === option ? 'correct' : (soru.selectedAnswer === option && !soru.correct ? 'incorrect' : '')}" onclick="return false;">${option}</button>`).join('')}</div>
                </div>
            `;

            soruOrnegiDiv.appendChild(soruOrnegi);
            soruListesiDiv.appendChild(soruOrnegiDiv);
        }
    }
}
 
Son düzenleyen: Moderatör:
Kod eksik galiba. showQuestions ile checkAnswer arasında bağ yok. Birbiri ile tetiklenen bir olay değiller. Dom load olduğunda showQuestions çalışıyor. Bir daha tetiklenmiyor.

Kodları doğru şekilde atarsan tekrar bakayım.
 

Geri
Yukarı