Why else part in radiocheck() fucntion running every time when i call it in another function

var quizdata = [

    {

        question: "who is the pm of indian",

        a: "rahul",

        b: "soniya",

        c: "akhilesh",

        d: "narendra modi",

        correct: 'modi'

    },

    {

        question: "who is zafar",

        a: 'developer',

        b: 'computer science student',

        c: 'nothing',

        d: 'none of these',

        correct: 'developer'

    }

];

let currentQuestion = 0;

// for loading the first`Preformatted text` question 

function quizload() {

        document.getElementById("questions").innerHTML = quizdata[currentQuestion].question;

        document.getElementById("a-text").innerHTML = quizdata[currentQuestion].a;

        document.getElementById("b-text").innerHTML = quizdata[currentQuestion].b;

        document.getElementById("c-text").innerHTML = quizdata[currentQuestion].c;

        document.getElementById("d-text").innerHTML = quizdata[currentQuestion].d;

        currentQuestion++;

}

quizload();  // calling 

//for changing the questions everytme .

document.getElementById("submitButton").addEventListener("click", () => {

    if (currentQuestion < quizdata.length) {
        radiocheck();
    }

    else {

        alert("thank for taking test");

    }

    document.querySelectorAll("input[name = 'answer']").checked = false;

});

// radio button validation is left

function radiocheck(){

    var temp = document.querySelectorAll("input[name = 'answer']");

    for(const val of temp){

        if(val.checked) {

           quizload();

           break;

        }

        else{

            alert("please selct ANSWER first");  //why this is poping  every timing

        }

    }

}

Hello there,

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


I recommend you edit your post to include more than just a wall of code in the body. That is, extra information and/or the question you have should be within the body of the post.

A post is less likely to illicit help, when all it contains is a wall of code.

You’re looping over your radio inputs. Unless the first input is checked (which will cause a break), it’ll go into the else part.

what should i change for expected output

I can’t tell you that, because I don’t know what the expected output is. Can you explain what your radiocheck function is supposed to do?
The easiest way to help you would be if you can provide an example of the whole code on https://codepen.io or similar.

There’s also an issue with this line:

document.querySelectorAll("input[name = 'answer']").checked = false;

querySelectorAll returns a node list. You can’t set the .checked attribute on a whole node list, you’ll have to loop over the items with .forEach or a for loop to set their .checked value individually.

thanks a lot it’s working now.