Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,679 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,186 people online right now. Registration is fast and FREE... Join Now!




Creating a random quiz

2 Pages V  1 2 >  
Reply to this topicStart new topic

Creating a random quiz

skin__
post 13 Sep, 2008 - 09:06 PM
Post #1


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

Hey guys, I'm trying to create a quiz that selects three questions out of six and then displays them. There's three different types of questions; single choice, multiple choice and writing. Here's what I have so far:

CODE
function createNewQuiz()
    {
        var questionSet = new Array(6);
        questionSet[0] = question_1;
        questionSet[1] = question_2;
        questionSet[2] = question_3;
        questionSet[3] = question_4;
        questionSet[4] = question_5;
        questionSet[5] = question_6;
        
        var question_1 = new Array();
        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
        
        var question_2 = new Array();
        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
        
        var question_3 = new Array();
        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
        
        var question_4 = new Array();
        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
        
        var question_5 = new Array();
        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
        
        var question_6 = new Array();
        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
        
        registerRandomQuestion();
    }

function registerRandomQuestion()
    {
        var totalQuestion = 6;
        
        randomNumber = Math.floor(Math.random()*totalQuestion);
        If(randomNumber != //stuck here)
        {
            createCookie(question_name, question_value, 1);
        }
    }

function createCookie(cookie_name, cookie_value, days)
    {
        document.cookie = cookie_value;
    }


I want it so that when registerRandomQuestion() is called it picks at random out of the six questions, but makes sure the question hasn't been picked before and then stores the random question to a cookie. I reckon I can work out how to then display the cookies one at a time on the webpage, but I'm really stuck with this bit at the moment!

Any help would be great.

This post has been edited by skin__: 14 Sep, 2008 - 12:27 AM
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 14 Sep, 2008 - 07:50 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


You have some serious problems with your code that will make it difficult to get it completed, but for the moment I believe I have added the functionality that you were looking for.

1 thing to realize is that when you have variables in a function (created in a function) they are normally just around for as long as the function is going, so it is best to create global variables to make sure that everything can use the variables that have been set.

Additionally you send question_name and question_value to create cookie, but they haven't ever been set so that will make it interesting.

Another thing to remember is the declare variables that are using other variables AFTER you have declared the variables that it is using. For instance:

CODE

var a = b;
var b = 5;


Doesn't work out, because b is undefined when you set a to it, meaning that a is undefined, then you set a value to b, so b is 5, and a is undefined.

Here is the code (which should work, haven't tested it though):
CODE
var question_1;
var question_2;
var question_3;
var question_4;
var question_5;
var question_6;
var questionSet;
var sel_questions = new Array();

function createNewQuiz(){        
        var question_1 = new Array();
        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
        
        var question_2 = new Array();
        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
        
        var question_3 = new Array();
        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
        
        var question_4 = new Array();
        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
        
        var question_5 = new Array();
        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
        
        var question_6 = new Array();
        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
        


        var questionSet = new Array(6);
        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()
    {
        var totalQuestion = 6;
        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, question_value, 1);
              return;
            }
          }
        }
    }

function createCookie(cookie_name, cookie_value, days)
    {
        document.cookie = cookie_value;
    }


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

skin__
post 16 Sep, 2008 - 01:45 AM
Post #3


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

Thanks heaps for that BetaWar. There's something wrong in the registerRandomQuestion() function, though. I've been trying to test if it works and every time I leave it in my code the tests don't work.

I've just been putting in a function called test with an alert in it to make sure the functions are being called properly and yeah, when registerRandomQuestion() is left in, it doesn't work. Can you see what's wrong with it? I can't and I've been playing around with it for a while. You're obviously much more experienced than me, though!

Can you please help me out with this?

Thanks heaps, BetaWar.
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 16 Sep, 2008 - 04:42 AM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Sorry, had a problem in my for loop.

Seems to be valid now:

CODE
function registerRandomQuestion()
    {
        var totalQuestion = 6;
        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, question_value, 1);
              return;
            }
          }
        }
    }
User is offlineProfile CardPM

Go to the top of the page

skin__
post 16 Sep, 2008 - 04:54 PM
Post #5


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

Thanks heaps, BetaWar. I just have one last question. I'm trying to test out the cookie value putting in some alerts to display their value. I'm using the code you've help me with and this as the createCookie function:

CODE
function createCookie(cookie_name, cookie_value, days)
    {
        document.cookie = cookie_value;
        alert(document.cookie);
    }


But it doesn't seem to be displaying anything. Can you help me with this one last thing? =P

Thanks!
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 16 Sep, 2008 - 05:14 PM
Post #6


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Sure, the cookie function needs both a cookie name and a value, all you are giving it is a name with no value (so it is automatically set to null).

Here is a version tha tseems to work (at least for my computer running IE 7):

CODE
<script>
function createCookie(cookie_name, cookie_value, days)
    {
        document.cookie = cookie_name+"="+cookie_value;
        alert(document.cookie);
    }
createCookie("test", 5, 100);
</script>


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

skin__
post 16 Sep, 2008 - 05:23 PM
Post #7


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

Thanks BetaWar, but that's not quite what I meant. I'm using this:
CODE
var question_1;
var question_2;
var question_3;
var question_4;
var question_5;
var question_6;
var questionSet;
var sel_questions = new Array();

function createNewQuiz()
{        
        var question_1 = new Array();
        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
        
        var question_2 = new Array();
        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
        
        var question_3 = new Array();
        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
        
        var question_4 = new Array();
        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
        
        var question_5 = new Array();
        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
        
        var question_6 = new Array();
        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
        
        var questionSet = new Array(6);
        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()
    {
        var totalQuestion = 6;
        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, question_value, 1);
              return;
            }
          }
        }
    }

function createCookie(cookie_name, cookie_value, days)
    {
        document.cookie = cookie_value;
    alert(document.cookie);
    }


And I'm trying to get it so that the alert displays what quiz question has been saved into that cookie, to make sure that it's working how I want it to. Or have I done it totally wrong? =P

Thanks BetaWar!
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 16 Sep, 2008 - 07:51 PM
Post #8


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


I guess I am not totally sure what you mean, but here is some useful documentation on Javascript cookies:

http://w3schools.com/js/js_cookies.asp

I quess you may mean when you are calling to the createCookie function here:

CODE
function registerRandomQuestion()
    {
        var totalQuestion = 6;
        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, question_value, 1);
              return;
            }
          }
        }
    }


The one big thing I notice if you are sending null variables (they haven't even been defined yet) to the cookie function, so that may cause some problems.

Here is what I would try:

CODE
function registerRandomQuestion()
    {
        var totalQuestion = 6;
        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;
            }
          }
        }
    }


But realize, that will be sending in a blank question_name variable, and an array for the question_value.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

skin__
post 16 Sep, 2008 - 10:39 PM
Post #9


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

Ahaha, thanks for the help BetaWar, I'm still trying to figure out what I'm actually doing myself. Basically now I want to have a way to test what values are being held sel_questions array. I've tried putting an alert in the createCookie function, but it's not working for some reason. I just want to make sure it's working the way it's supposed to, but I can't figure out a way to get an output.

What would be the best way to do this?
Thanks heaps BetaWarrrrrr.
User is offlineProfile CardPM

Go to the top of the page

skin__
post 21 Sep, 2008 - 05:29 PM
Post #10


New D.I.C Head

*
Joined: 20 May, 2008
Posts: 37

BUMP:
Anyone?
User is offlineProfile CardPM

Go to the top of the page

Rastafubu
post 2 Oct, 2008 - 12:34 AM
Post #11


New D.I.C Head

*
Joined: 1 Oct, 2008
Posts: 2

hey skin


were yu able to complete the website for beijing olympics. cause am doin the same thing and am stuck oncallin the form for ristration and beyond
hopin if yu completed it yu can send me the js codes and html codes ...really need yr help


this is my html
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Beijing Olympic Quiz</title>
<script src='file:///Homes/public_html/week4/Assgnmnt 2 DWD/function.js' type='text/Javascript' />
<style type="text/css">
<!--
.style11 {
    font-family: "Monotype Corsiva";
    font-size: 60px;
}
.style12 {
    color: #000000;
    font-size: 24px;
}
-->
</style>
</head>

<body>


  <table width="963" height="12" border="0" align="center">
    <tr>
      <td width="230" height="245"><p><img src="file:///Homes/public_html/week4/Assgnmnt 2 DWD/images/beijing olympics.gif" alt="logo" width="210" height="204" /></p>

      <p>&nbsp;</p></td>
      <td width="677" bgcolor="#CCCCCC"><span class="banner style11"> BEIJING OLYMPICS QUIZ   </span></td>
    </tr>
  </table>
  <table width="962" border="0" align="center">
    <tr>
      <td width="230" background="file:///Homes/public_html/week4/Assgnmnt 2 DWD/ " bgcolor="#CCCCCC"><div align="center">
        <p class="style1 style12">WELCOME </p>

        <p class="style1 style12">TO </p>
        <p class="style1 style12">BEIJING 2008  </p>
      </div></td>
      <td width="676"><div align="center">
        <h2><span class="style1">Now try our wonderful Beijing Quiz !!!!!!! </span></h2>
        <div align="center">
           <p align="left"><span class="style9">Did you enjoy Beijing Olympics?
            </span></p>

           <p align="left" class="style9">Its time for reviewing facts about Beijing Olympics.</p>
           <p align="left" class="style9">To try this quiz you need to provide your name and email address,for </p>
          <p align="left" class="style9"><span class="style9">generating personal certificate</span> </span><span class="style5">.</span> http://www.dreamincode.net/forums/showtopic63033.htm</p>
          <p align="center" class="style9"><a href="#" onclick="displayRegistrationForm()">
                CLICK HERE FOR REGISTRATION</a> </p>

        </div>
      </div></td>
    </tr>
</table>
  <table width="960" border="1" align="center">
    <tr bgcolor="#FFFFFF">
      <td><p align="center">This Beijing Olympics Website was created courtesey of : NIGEL PANGANAI CHIGWIDA </p></td>
    </tr>
</table>

  <p>&nbsp;</p>

</body>
</html>


my JS code
CODE

// JavaScript Document
function displayRegistrationForm()
{
var strHTML = "Registration Form";
document.getElementById('content').innerHTML = strHTML;  
}

User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 2 Oct, 2008 - 04:41 AM
Post #12


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


Here are a few cookie functions that should work (got them from w3schools)

CODE
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}function setCookie(c_name,value,expiredays)
{var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
  else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}


Basically checkCookie sees if a cookie has been set, setCookie sets a cookie value (notice it needs a cookie name and value) and then getCookie returns the value of a cookie when you pass in the name of that cookie.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:35AM