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 2 of 2

Thread: Cookie help

  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookie help

    Hey guys, I hope someone can help me with this.
    I'm trying to create a quiz in Javascript. The questions are stored in a multi-dimension array and are chosen at random (three questions are chosen out of six).

    The questions are then stored in their own cookies and saved to be displayed later. Here's what I have so far:

    Code:
    var question_1 = new Array();
    var question_2 = new Array();
    var question_3 = new Array();
    var question_4 = new Array();
    var question_5 = new Array();
    var question_6 = new Array();
    var questionSet = new Array();
    var sel_questions = new Array();
    
    function createNewQuiz()
    {        
            question_1[0] = 1; // single choice
            question_1[1] = "Who hosted 2008 Olympics?";
            question_1[2] = "China";
            question_1[3] = "USA";
            question_1[4] = "Australia";
            question_1[5] = "UK";
            question_1[6] = 2; // answer
            
            question_2[0] = 1; // single choice
            question_2[1] = "How many gold medals Australia did obtained?";
            question_2[2] = "13";
            question_2[3] = "14";
            question_2[4] = "15";
            question_2[5] = "16";
            question_2[6] = 3; //answer
            
            question_3[0] = 2; // multi-choice
            question_3[1] = "Choose at least two disciplines that Australia    obtained gold medals.";
            question_3[2] = "Basketball";
            question_3[3] = "Diving";
            question_3[4] = "Swimming";
            question_3[5] = "Rowing";
            question_3[6] = "3,4,5"; //answer index
            
            question_4[0] = 2; // multi-choice
            question_4[1] = "Choose at least two basketball players of USA team.";
            question_4[2] = "KIDD Jason";
            question_4[3] = "RUBIO Ricky";
            question_4[4] = "BOSH Chris";
            question_4[5] = "PRIGIONI Pablo";
            question_4[6] = "2,4"; //answer index
            
            question_5[0] = 3; // writing
            question_5[1] = "Choose three disciplines that Australia obtained gold medals.";
            question_5[2] = "Athletics, Basketball, Rowing, Sailing, Diving, Softball, Swimming";
            question_5[3] = "Athletics, Rowing, Sailing, Diving, Swimming";// Answers
            
            question_6[0] = 3; // writing
            question_6[1] = "Choose at least three swimmers.";
            question_6[2] = "MURPHY Patrick, RYAN Matt, McKENZIE-McHARG Cameron, MARBURG James, HACKETT Grant, BRITS Grant, FFROST Nick, PALMER Kirk, HEGERTY Francis, BRODIE Leith";
            question_6[3] = "MURPHY Patrick, HACKETT Grant, BRITS Grant, FFROST Nick, PALMER Kirk, BRODIE Leith";// Answers
            
            questionSet[0] = question_1;
            questionSet[1] = question_2;
            questionSet[2] = question_3;
            questionSet[3] = question_4;
            questionSet[4] = question_5;
            questionSet[5] = question_6;
    
            registerRandomQuestion();
        }
    
    function registerRandomQuestion()
        {
            for(rand = Math.floor(Math.random()*questionSet.length); rand>=0; rand = Math.floor((Math.random()*questionSet.length)))
            {
              for(i = rand; i<sel_questions.length; i++)
              {
                if(sel_questions[i] == rand)
                {
    				break;
                }
                else
                {
    				sel_questions[sel_questions.length] = rand;
    				createCookie(question_name, sel_questions[i], 1);
    				return;
                }
              }
            }
        }
    
    function createCookie(cookie_name, cookie_value, days)
        {
            document.cookie = cookie_value;
        }
    It's pretty messy at the moment and very incomplete, but I'm totally lost. I guess for the next step I'd like to somehow check the contents of the cookie to make sure they're being saved properly. I've been trying to do this for a while and can't seem to get it to work.

    Please help me!

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    try using existing utility functions like http://www.quirksmode.org/js/cookies.html
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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