Solved

Variable not being assigned correctly...

Posted on 2008-10-08
5
221 Views
Last Modified: 2012-05-05
I am attempting to get a random question to display on a page. Basically the variable question should be taken by createNewQuestion() and give a result based on what question type it is. (this value is stored in an array, question will be an array as the first variable is assigned from an array of array's, this is all documented in the code snippet.

basically at the top of the document question is assigned as a global variable with a value of "". later it SHOULD be assigned as an array and should be a random array at that. However, as the screenshot show's, the result is coming back as undefined. if it was selecting an array, the radio buttons and title would have been filled in. i tested the question variable and found that when it was put on the screen, even though within createNewQuestion it has been assigned an array, it still display's nothing. I believe it is still carrying a value of "". any help regarding why question is not being assigned to an array properly would be fantastic.

thankyou for your time :)
function displayQuizInstruction()

{
 

  

  var inst= '<h3>Beijing Quiz Instruction</h3> <p>Hi</p>' + displayName + '<p>This quiz consists of three questions from question pools. You can refer to relevant information from the Beijing 2008 official webpage, especially the <a href="http://results.beijing2008.cn/WRM/ENG/INF/GL/95A/GL0000000.shtml">overall medal tally</a> to provide you with useful information.</p> <p>At the end of the quiz, the system will display a certificate telling you how well you went.</p> <p>Do you want to try the quiz?</p><button type=\"button\" onclick= \"createNewQuestions();\">Start Quiz</button>';

  

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

}
 

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();

}
 

function createQuizDisplay()

{

  
 

  if(question[0]= 1) 

  { 

    template = '<h3>'+ question[1] + '</h3> <form id= \"questionTemplate\" action= \"\"> <fieldset><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> <button type= \"button\" id=\"saveAnswer\">Save Answer</button> <button type= \"button\" id="\next\">Next >>></button>'

  }

  

  else if(question[0]= 2)

  { 

    template= '<h3>question[1]</h3> <form id=\"questionTemplate\" 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=\"questionTemplate\" action=\"\"> <fieldset> <legend>question[2]</legend> \"Answers:\" <br /> <input type=\"text\"  id=\"3temp1\" size=\"50\" name= question[0] /> </fieldset> </form>'

  }
 

}
 

function displayQuiz()

{
 

  createQuizDisplay();

  

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

}

Open in new window

coderesult.jpg
0
Comment
Question by:Dan09
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:priya_patil
ID: 22668306
i am really not sure how do yo call it n display it
but if you want to use question as global variable declare it below script tag and before any function definition

and i guess question is array so declare it as
var question= new Array();

and write
question=first (i guess that first also contains array value)
0
 

Author Comment

by:Dan09
ID: 22668418
Hmm, changing the instantiation of the question variable when it was first declared did not help. I have linked more substantial snippets of the code this time, and commented what it is supposed to be doing.

Hopefully this will be clearer what the travels of the question variable are and what it should end up being.

Sorry if this is confusing for you, but it has really got me stumped. question should have the value of an array stored in it via question = first; but it still seems to be coming up blank.

thanks once again.
var displayName= "";

var question= "";

var template= "";

//these are the global variable I create at the beginning of the.js file.
 

  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;

//this is where question is given a value...should be an array.
 

function createQuizDisplay()

{

  
 

function createQuizDisplay()

{

  
 

  if(question[0]= 1) 

  { 

    template = '<h3>'+ question[1] + '</h3> <form id= \"questionTemplate\" action= \"\"> <fieldset><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> <button type= \"button\" id=\"saveAnswer\">Save Answer</button> <button type= \"button\" id="\next\">Next >>></button>'

  }

  

  else if(question[0]= 2)

  { 

    template= '<h3>question[1]</h3> <form id=\"questionTemplate\" 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=\"questionTemplate\" action=\"\"> <fieldset> <legend>question[2]</legend> \"Answers:\" <br /> <input type=\"text\"  id=\"3temp1\" size=\"50\" name= question[0] /> </fieldset> </form>'

  }
 

}

//this function chooses what to display based on the question variable.
 

function displayQuiz()

{
 

  createQuizDisplay();

  

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

}

//this is where it is put on the screen.
 

var questionSet = new Array(6);

  questionSet[0]=question1;

  questionSet[1]=question2;

  questionSet[2]=question3;

  questionSet[3]=question4;

  questionSet[4]=question5;

  questionSet[5]=question6;

//controlling array.
 

var question1 = new Array();

  question1[0]=1;                                    //Question Type

  question1[1]="Who hosted the 2008 Olympic Games?";    //Possible Answer

  question1[2]="China";

  question1[3]="USA";

  question1[4]="Australia";

  question1[5]="Great Britain";

  question1[6]=2;    

//e.g of question array. question should end up being assigned to this 

Open in new window

0
 

Author Comment

by:Dan09
ID: 22668452
Hmm, changing the instantiation of the question variable when it was first declared did not help. I have linked more substantial snippets of the code this time, and commented what it is supposed to be doing.

Hopefully this will be clearer what the travels of the question variable are and what it should end up being.

Sorry if this is confusing for you, but it has really got me stumped. question should have the value of an array stored in it via question = first; but it still seems to be coming up blank.

also this .js file is separate from my .html and .css files, if that makes any difference in this scenario.

thanks once again.
var displayName= "";

var question= "";

var template= "";

//these are the global variable I create at the beginning of the.js file.
 

  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;

//this is where question is given a value...should be an array.
 
 

function createQuizDisplay()

{

  
 

  if(question[0]= 1) 

  { 

    template = '<h3>'+ question[1] + '</h3> <form id= \"questionTemplate\" action= \"\"> <fieldset><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> <button type= \"button\" id=\"saveAnswer\">Save Answer</button> <button type= \"button\" id="\next\">Next >>></button>'

  }

  

  else if(question[0]= 2)

  { 

    template= '<h3>question[1]</h3> <form id=\"questionTemplate\" 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=\"questionTemplate\" action=\"\"> <fieldset> <legend>question[2]</legend> \"Answers:\" <br /> <input type=\"text\"  id=\"3temp1\" size=\"50\" name= question[0] /> </fieldset> </form>'

  }
 

}

//this function chooses what to display based on the question variable.
 

function displayQuiz()

{
 

  createQuizDisplay();

  

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

}

//this is where it is put on the screen.
 

var questionSet = new Array(6);

  questionSet[0]=question1;

  questionSet[1]=question2;

  questionSet[2]=question3;

  questionSet[3]=question4;

  questionSet[4]=question5;

  questionSet[5]=question6;

//controlling array.
 

var question1 = new Array();

  question1[0]=1;                                    //Question Type

  question1[1]="Who hosted the 2008 Olympic Games?";    //Possible Answer

  question1[2]="China";

  question1[3]="USA";

  question1[4]="Australia";

  question1[5]="Great Britain";

  question1[6]=2;    

//e.g of question array. question should end up being assigned to this 

Open in new window

0
 
LVL 2

Expert Comment

by:priya_patil
ID: 22684334
hey i am still not getting how do you use this
but when i use your code and i directly call your displayQuiz function on Onload method
the questions are coming

as i said var first is getting some aray value
check your readcookie and createcookie function
as you have not given the code
i just commented it while testing
so first test what are you getting as value of first

other thing
add a + before question[0]
template= '<input type=\"checkbox\" id=\"2temp1\" name='+question[0]+' value='+question[2]+'/>'+question[2]

and use == instead of =
i.e.
if(question[0]==2)

it would be better if you can attach jsp code
debugging would be so easy then
still you can try this
0
 

Accepted Solution

by:
Dan09 earned 0 total points
ID: 22685740
I have since managed to stumble across a solution. Thankyou for your time though. The problem was that I had defined question within a new scope, which then played no part outside of it.

All I needed to do was change var question= first; to question= first;

But thankyou for your time and I will start using the ==  :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now