Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    variable not ending up with assigned value:

    Hey everyone. I'm new here and so if i make a mistake or two please forgive me, but i'll try my best to make this post coherent :)

    Ok so I am attempting to make a quiz type arrangement, using arrays to store questions and answers. The idea is to select a random question and put it on the screen. I have done this properly as far as I can tell, but where I seem to keep falling down is calling the variable to put on the screen. Having tested it as thoroughly as I know how what I am seeing is that the variable I have assigned to have the value in the array I want to display is either coming back undefined or empty (it was set to empty at the beginning of the script as I am calling it in a couple of different functions). I would appreciate any help on how to get this variable to have the actual value i assigned to it. I'll link the appropriate code below:

    <button type=\"button\" onclick= \"createNewQuestions();\">Start Quiz</button>'; //this starts the ball rolling

    function createNewQuestions()
    {

    var totalQuestion= 6;
    var question1;
    var question2;
    var question3;

    question1= Math.floor(Math.random()*totalQuestion);

    if(question1 != null)
    {

    question2= Math.floor(Math.random()*totalQuestion);

    }

    while(question2 == question1)
    {

    question2= Math.floor(Math.random()*totalQuestion);

    }

    if(question2 != null)
    {

    question3= Math.floor(Math.random()*totalQuestion);

    }

    while(question3 == question1 || question3 == question2)
    {

    question3= Math.floor(Math.random()*totalQuestion);

    }

    createCookie("question1", question1, 3, "", "", "");
    createCookie("question2", question2, 3, "", "", "");
    createCookie("question3", question3, 3, "", "", "");

    var qc1= readCookie("question1");
    var qc2= readCookie("question2");
    var qc3= readCookie("question3");

    var first= questionSet[qc1];
    var second= questionSet[qc2];
    var third= questionSet[qc3];

    var question= first;

    displayQuiz();
    }
    //this is used to get a random question from my array's. The array values are all set and I don't think i need to show them here. The key may be the order in which I have placed these functions coming up. It is linked exactly as it is in my .js file, although I made a conscious effort not to have functions firing ahead of time. anyway, the next function isn't called next but i have written it next:

    function createQuizDisplay()
    {


    if(question[0]= 1)
    {
    template = '<input type=\"radio\" id=\"1temp1\" name= question[0] value= question[2] />question[2] <br /> <input type=\"radio\" id=\"1temp2\" name= question[0] value= question[3] />question[3] <br /> <input type=\"radio\" id=\"1temp3\" name=question[0] value= question[4] />question[4] <br /> <input type=\"radio\" id=\"1temp4\" name= question[0] value= question[5] />question[5] </fieldset> </form>'
    }

    else if(question[0]= 2)
    {
    template= '<h3>question[1]</h3> <form id=\"questionTemplate2\" action=\"\"> <fieldset> <input type=\"checkbox\" id=\"2temp1\" name= question[0] value= question[2] />question[2] <br /> <input type=\"checkbox\" id=\"2temp2\" name= question[0] value= question[3] />question[3] <br /> <input type=\"checkbox\" id=\"2temp3\" name= question[0] value= question[4] />question[4] <br /> <input type=\"checkbox\" id=\"2temp4\" name= question[0] value= question[5] />question[5] </fieldset> </form>'
    }

    else
    {
    template= '<h3>question[1]</h3> <form id=\"questionTemplate3\" action=\"\"> <fieldset> <legend>question[2]</legend> \"Answers:\" <br /> <input type=\"text\" id=\"3temp1\" size=\"50\" name= question[0] /> </fieldset> </form>'
    }

    }
    //this function basically asks my questionSet array what type of question it is, and depending on the value it gets will display a different format. This particular function has a lot wrong with it and I haven't really ironed out the kinks, but it should still show a messed up version of what I want despite not the exact values (i.e it should show gobbeldygook on the screen, but random gobbeldygook depending on what random value I got from my math.random function earlier)

    the next function is where it seems to break down:

    function displayQuiz()
    {

    createQuizDisplay();

    document.getElementById("main").innerHTML= template;

    }
    //now, this function is used to put what i want on the screen, and when it is run, It will consistently give only the answer from the first if statement in the last function. However this should be throwing a random result on the screen each time from 1 of the 3 possibilites, if, if else or else.

    Upon trying to find out where it became unstuck, I found out that question has a value of "" or nothing, if it is put on the screen (.innerHTML = question;) I did this to see if there was an error in my createQuizDisplay() function (of which there are plenty) BUT, the particular error which is tripping me up seems to be elsewhere, I assume in the createNewQuestions() function as question is supposed to be given a value of an array (var question = first;). But on testing question is null. I also made first a global variable, instantiating it with a nothing value (var first= "";) and when i tried to display this on screen to see if first was where I went wrong I got the same result (an empty screen, meaning first still carried a value of ""...I think)

    Where I think the problem lies is that somehow question is keeping it's initial value which has been set to empty (var question= "";).

    I don't have any other idea's, so if anyone could shed any light that would be a massive help :) I just need question to have the value of the array that I asked it to have in createNewQuestions() and I will be good to go.

    Thanks in advance. Sorry for such a long post.

    Here is some stuff that may have been confusing:

    questionSet
    is an array containing array's, so var first= questionSet[qc1]; should ideally return an array.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    It will be a lot easier to see the markup and scripts involved in complete settings unaltered.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rangana View Post
    It will be a lot easier to see the markup and scripts involved in complete settings unaltered.
    hmm, I was trying to show as little code as possible, just what I felt was relevant. Would it be more helpful to show the whole thing?

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bump. this really has me stumped


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •