Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:39:16+00:00 2026-06-18T07:39:16+00:00

This script is giving me problems. I’ve re written it several times but I’m

  • 0

This script is giving me problems. I’ve re written it several times but I’m missing something.

‘questions’ array refers to my objects. These objects are different questions with ‘a’ always being the correct answer.

The script works fine up to this point. The idea is to increment ‘n’ from 0 to scroll through my array, and provide the next question when a correct answer is clicked. ‘x’ is always the correct answer (it always holds ‘a’).

So I can increment ‘n’ just fine on correct guess; however, when I call the function ‘a()’ again, (after my alert ‘hi’ part), it is causing problems. I want to increment n, and call a() so I get the next question. Then I want to place the correct guess (x) in a random place (ie position 0, 1 or 2.)

Grateful for any help.

var questions = [q0,q1,q2,q3,q4,q5,q6,q7];

var n = 0;

function a(){
var y;
var z;

var x = Math.floor((Math.random() * 3))
if(x == 0){y = 1; z = 2}else if(x == 1){y = 0; z = 2}else{y = 0; z = 1}

$("#question_holder").text(questions[n].q);

    $(".answer_holder").eq(x).text(questions[n].a);
    $(".answer_holder").eq(y).text(questions[n].b);
    $(".answer_holder").eq(z).text(questions[n].c);


    $(document).ready(function(){

        $(".answer_holder").eq(x).click(function(){
            alert("hi");
                        n++;
            a();

                     /*this area needs to get the next question by incrementing
                     n, then generate a different value to x, to place it
                     in a different position. which it does. however,
                     in subsequent questions, you can click the wrong answer as if
                     it's correct, ie not 'a' or 'x'.*/
        });
    });
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:39:17+00:00Added an answer on June 18, 2026 at 7:39 am

    You have the $(document).ready() in the wrong place. Try something like this (caveat: this is completely untested):

    function setupQuestion(n) {
        var x,y,z;
    
        x = Math.floor((Math.random() * 3))
        if(x == 0){y = 1; z = 2}else if(x == 1){y = 0; z = 2}else{y = 0; z = 1}
    
        $("#question_holder").text(questions[n].q);
    
        $(".answer_holder").eq(x).text(questions[n].a).data('answer', 'a');
        $(".answer_holder").eq(y).text(questions[n].b).data('answer', 'b');
        $(".answer_holder").eq(z).text(questions[n].c).data('answer', 'c');
    }
    
    $(document).ready(function() {
        var n = 0;
        $('.answer_holder').click(function() {
            if ($(this).data('answer') === 'a') { // Or whatever is the correct answer
                n++;
                if (n < questions.length) {
                    setupQuestion(n);
                } else {
                    // Do something else, the test is finished
                }
            }
            return false;
        });
        setupQuestion(n);
    });
    

    Note that I am not comparing on the text() function but rather the data() function. This is because the text as displayed to the user might be decorated in some way, making comparisons difficult. Simply using the answer index ‘a’ ‘b’ or ‘c’ is clearer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A php script is giving this array (which has been passed through json_encode() )
This script works fine on single click but on double click it shows both
This simple Perl script is translating stories from a database into XML, but this
This JS is giving me some problems. Specifically FF3.5 is saying there is Error:
I am trying to run a Node.js script locally and it's giving me this
This script is giving error if name field and file field is same. That
This function is giving me trouble running something similar to the example provided at:
This script saves the files as /home/name/main/all my files and stuff . I want
This script cannot find bannersize unless I write <div id=bannersize></div> before the javascript. The
This script successfully generated the pdfs to a folder tmp/.... However the ZIP output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.