Akamaru
Akamaru

Reputation: 83

JavaScript quiz redirect issue

I'm trying to code a quiz which will redirect to one of two secondary quizzes depending on the answers chosen. I have the below code which I thought would work but instead it either completes the else return or reloads the quiz. The secondary quiz is located within ques2.html and the third within ques3.html. Does anyone have any ideas what I'm doing wrong, or have any potential solutions?

var data = {
    questions: [{
            question: "Which interests you most?",
            options: [ "Medicine" , "Physics" ]
        }, {
            question: "Which interests you most?",
            options: [ "Vet/Animals" , "Astrology"]
        }, {
            question: "Which interests you most?",
            options: [ "Nutrition" , "Chemistry"]
        }, {
            question: "Which interests you most?",
            options: [ "Psychology" , "Forensic Science" ]
        }, {
            question: "Which interests you most",
            options: [ "Dentistry" , "Biology"]

        }],

};

var qIndex = 0,
    index = 0,
    aString = "",
    maxQ = 5,
    q = document.getElementById('ques'),
    opt1 = document.getElementById('opt1'),
    opt2 = document.getElementById('opt2');

function generate(){
    q.innerHTML= data.questions[qIndex].question;
    opt1.value= data.questions[qIndex].options[0];
    opt2.value= data.questions[qIndex].options[1];
}

function optionClick(ans) {
    if(qIndex%1==0){
        aString+=ans;
        index++;
    }
    qIndex++;
    if(qIndex == maxQ){

        redirect( generateResult );
    } else{
        generate();
    }
}

function generateResult(answers) {
    if( answers =="00000" || answers =="00001" || answers=="00010" || answers=="00100" || answers=="01000" || answers=="10000" || answers=="00011" || answers=="00110" || answers=="01100" || answers=="11000" || answers=="00101" || answers=="01010" || answers=="10100" || answers=="01001" || answers=="10010" || answers=="10001" ) return "ques2.html";
    else if( answers=="11111" || answers=="11110" || answers=="11101" || answers=="11011" || answers=="10111" || answers=="01111" || answers=="11100" || answers=="11001" || answers=="10011" || answers=="00111" || answers=="11010" || answers=="10101" || answers=="01011" || answers=="10110" || answers=="01101" || answers=="01110" ) return "ques3.html";
    else return "new.html"; 
}


function redirect(url) {
    window.location = url;
}

Upvotes: 0

Views: 87

Answers (2)

Stephen P
Stephen P

Reputation: 14820

In the code you posted in your question, you do

redirect( generateResult );

... but here, generateResult is a reference to the function, it is not calling the function, so you are passing a javascript function to redirect(...), you are not passing a URL.

To call the function you need the parentheses

redirect( generateResult() );

-- but then, generateResult is expecting a parameter answers as we see in its declaration — so, you need to pass the string you have been building up, aString, in your optionClick(ans) function

redirect( generateResult(aString) );

Also, I'm not sure what you're trying to do with this test in optionClick...

function optionClick(ans) {
if(qIndex%1==0){
    aString+=ans;
    index++;
}

... but anything N % 1 always leaves a remainder of zero, so qIndex%1==0 is always true and those two lines are always executed.
Clearly qIndex is used, but it looks like index is not, and could be removed.

In any case, I've created a fiddle where I've stubbed out the functions that interact with the page (since you don't show any html) and just writes to the browser console. It simulates having clicked on options in order: 0, 1, 0, 0, 0 and correctly outputs that it would go to page ques2.html.

An update to the fiddle strips out the parts that are unnecessary, based on what I've written above, and shows a different sequence: 0,1,1,0,1 which outputs ques3.html.

Upvotes: 0

aleksa95
aleksa95

Reputation: 69

When you call your redirect function you have to pass it generateResult with a propertie like this:

 redirect( generateResult(anwsers) );

Upvotes: 1

Related Questions