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!
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_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;
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
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
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!
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 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
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
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.
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.
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
<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> </p>
</body> </html>
my JS code
CODE
// JavaScript Document function displayRegistrationForm() { var strHTML = "Registration Form"; document.getElementById('content').innerHTML = strHTML; }
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.