Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Help Javascript random generator
Old 08-02-2008, 09:28 PM Help Javascript random generator
Novice Talker

Posts: 7
Trades: 0
[help] Javascript Random Generator

Hi all, Sorry to bother u all, I think i previously post about the quiz script, and i read and research abit here and there,

anyway i manage to write some but found some difficulties,especially about random generator..

I have this several 'set' of question and answer, and i like to view'em random
(e.g: refresh---> What is 1+1=?...with option n aswers, refresh---> What is 1+1=?...with option n aswers, refresh---> What is

1+1=?...with option n aswers, ...etc)

Code:
questionarray[0]  ='What is 1+1=?.' 
answer[0]  ='1' 
Adistract[0]  ='2' 
Bdistract[0]  ='22' 
Cdistract[0]  ='11' 
Ddistract[0]  ='0'  
questionarray[1]  ='what is the color of the sky?.' 
answer[1]  ='C' 
Adistract[1]  ='red' 
Bdistract[1]  ='yellow' 
Cdistract[1]  ='blue' 
Ddistract[1]  ='purple'  
questionarray[2]  ='cow is =?.' 
answer[2]  ='D' 
Adistract[2]  ='Tank' 
Bdistract[2]  ='President' 
Cdistract[2]  ='Weapon' 
Ddistract[2]  ='Animal'  
questionarray[3]  ='What is 2+2=?.' 
answer[3]  ='A' 
Adistract[3]  ='4' 
Bdistract[3]  ='22' 
Cdistract[3]  ='11' 
Ddistract[3]  ='0'  .....etc
i know this can be done by
Code:
var i = Math.round(6*Math.random());
document.write(stemarray[i]);
but when i use that, i only get the question...i can't randomly view them as 'set' [question,option of answers and the right

answer].

and also if any of master n gurus here have better suggestion, both on the question array and get (viewing method on browser),

i'll be very thankful.
Anyway, thanks in advance.

Best Regards

~M~

Last edited by mamamiaw; 08-03-2008 at 03:09 AM..
mamamiaw is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-03-2008, 06:54 AM Re: Help Javascript random generator
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Is this intended to be a real world application or simply a "proof of concept"?

because as was pointed out in your previous thread -> http://www.webmaster-talk.com/javasc...ript-quiz.html

using javascript for this is simply NOT a viable option.

EVERYBODY will be able to win EVERYTHING, simply because the ALL the questions AND answers are in plain view in the source code.

the concept simply has to be secure enough to ensure the security and integrity of the Q & A system. Otherwise is not a quiz, but a game of hide and seek with nobody actually hiding.

The proof of concept is very simple and could be a useful exercise in learning about DOM scripting in general, but as a real quiz, with real prizes it's a total non-starter.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-03-2008, 09:40 AM Re: Help Javascript random generator
Novice Talker

Posts: 7
Trades: 0
yes i realize that also, so it's only a simple game simulation...the user won't be able to see the code because it's not online and they cant touch anything (no mouse or keyboard)...the user will be my student by the way, they only sit on their chair and pick the answer...

it's not a commercial project, it's not even a project at all, it's just a method of teaching, that's all

so, do you (or any of master here ) have any solution or recommendation?

anyway thanks for replyin..
mamamiaw is offline
Reply With Quote
View Public Profile
 
Old 08-03-2008, 10:54 AM Re: Help Javascript random generator
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok then, it's not a big problem. Just there is some disconnect between the posted code though.

Where exactly does stemarray actually come from?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-03-2008, 07:35 PM Re: Help Javascript random generator
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok been decorating the kids bedrooms today, so wanted something to do in between watching the paint dry .

http://www.candsdesign.co.uk/demo/ajax-demo/

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Who wants to be a millionaire?</title>
<script type="text/javascript">
var xmlQandA = null;
var isIE = false
var answer = null;
var canSetCorrect = false;
var canSetSelect = true;
  
function getQandA()
{
try //IE
  {
  xmlQandA=new ActiveXObject("Microsoft.XMLDOM");
  isIE = true;
  }
catch(e)
  {
  try //The good ones.
    {
    xmlQandA=document.implementation.createDocument("","",null);
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
xmlQandA.async=false;
xmlQandA.load("qanda.xml");
var count = xmlQandA.getElementsByTagName("group")
var refNo = rnd(count.length)
var grp = xmlQandA.getElementsByTagName("group")[refNo];
var children = grp.childNodes;

// take note that IE set XML nodes differently to the rest of the world 
	if (isIE) {	
		answer = children[1].text;
		document.getElementById("question").innerHTML= children[0].text;
		document.getElementById("ans_a").innerHTML= children[2].text;
		document.getElementById("ans_b").innerHTML= children[3].text;
		document.getElementById("ans_c").innerHTML= children[4].text;
		document.getElementById("ans_d").innerHTML= children[5].text;
	} else {
		answer = children[3].textContent;
		document.getElementById("question").innerHTML= children[1].textContent;
		document.getElementById("ans_a").innerHTML= children[5].textContent;
		document.getElementById("ans_b").innerHTML= children[7].textContent;
		document.getElementById("ans_c").innerHTML= children[9].textContent;
		document.getElementById("ans_d").innerHTML= children[11].textContent;
	}

}
function setDeselected() {
		document.getElementById("ans_a").className = "normal";
		document.getElementById("ans_b").className = "normal";
		document.getElementById("ans_c").className = "normal";
		document.getElementById("ans_d").className = "normal";
}

function setSelected(objID) {
	if (canSetSelect) {
	setDeselected()
	document.getElementById("ans_" + objID).className = "selected";
	setCorrect = true;
	canSetSelect = false;
	}
}

function showCorrect() {
	if (setCorrect) {
		document.getElementById("ans_" + answer).className = "correct";	
	}
}

function setNext(obj) {
	canSetSelect = true;
	setCorrect = false;
	setDeselected();
	getQandA();
}

function rnd(p_iCount) {
return Math.round( (p_iCount-1) * Math.random());
}



</script>
<style type="text/css">
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
#quiz {
	width:50%;
	border:0.1em solid #99CC33;
	margin:20px auto;
	text-align:center;
	cursor:pointer;
}
#question {
	font-size:1.2em;
	font-weight:bold;
	width:100%;
	margin:1px auto;
}
div.letter {
	font-size:1.3em;
	font-weight:bold;
	width:5%;
	float:left;
	color: #FF6600;
	background-color:#ffffff
}
#ans_a, #ans_b, #ans_c, #ans_d  {
	width:42%;
	margin:2px auto;
	font-weight:bold;
	float:left;
}
.row {
	width:100%;
	clear:left;
	background-color: #FFCC33;
}
.normal {
	color: #000033;
	background-color: #FFCC33;
}
.selected {
	color: #ffffff;
	background-color: #FF0000;
	text-decoration: blink;
}
.correct {
	color: #000066;
	background-color: #66CC00;
	text-decoration: blink;
}
</style>
</head>

<body>
<div id="quiz">
<div id="question" onclick="setNext()">Question</div>

<div class="row">
	<div id="a" onclick="setSelected(this.id)" class="letter">A</div>
	<div id="ans_a" class="normal" onclick="showCorrect()">Answer 1</div>
	<div id="b" onclick="setSelected(this.id)" class="letter">B</div>
	<div id="ans_b" class="normal" onclick="showCorrect()">Answer 2</div>
</div>
<div class="row">
	<div id="c" onclick="setSelected(this.id)" class="letter">C</div>
	<div id="ans_c" class="normal" onclick="showCorrect()">Answer 3</div>
	<div id="d" onclick="setSelected(this.id)" class="letter">D</div>
	<div id="ans_d" class="normal" onclick="showCorrect()">Answer 4</div>
</div>
<div style="clear:both"></div>
</div>

</body>

</html>
<script type="text/javascript">setNext()</script>
the XML Data
Code:
<?xml version="1.0" ?> 
<qanda>
	<group name="001" id="001">
		<question>What is 3 + 3</question>
		<answer>c</answer>
		<opt name="a">12</opt>
		<opt name="b">4</opt>
		<opt name="c">6</opt>
		<opt name="d">9</opt>
	</group>
	<group name="002" id="002">
		<question>What is the capital of France</question>
		<answer>a</answer>
		<opt name="a">Paris</opt>
		<opt name="b">London</opt>
		<opt name="c">Istanbul</opt>
		<opt name="d">Moscow</opt>
	</group>
	<group name="003" id="003">
		<question>Is a whale a</question>
		<answer>c</answer>
		<opt name="a">Rodent</opt>
		<opt name="b">Fish</opt>
		<opt name="c">Mammal</opt>
		<opt name="d">Reptile</opt>
	</group>
	<group name="004" id="004">
		<question>What does the chemical symbol H2O represent?</question>
		<answer>d</answer>
		<opt name="a">Orange</opt>
		<opt name="b">Sulphuric Acid</opt>
		<opt name="c">Salt</opt>
		<opt name="d">Water</opt>
	</group>
	<group name="005" id="005">
		<question>What country does Caviar come from</question>
		<answer>d</answer>
		<opt name="a">Denmark</opt>
		<opt name="b">Brazil</opt>
		<opt name="c">Belgium</opt>
		<opt name="d">Russia</opt>
	</group>
</qanda>
bit on the basic side but heyho

click on the question to select a new random question (or refresh the page)

Click on a letter to select an answer. The background of the selected answer turns red and it will lock you out of changing the selection.

Click on any answer to show the correct one. The background of the correct answer turns green.

Extend the questions by adding more Q & A groups in the XML document.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-04-2008, 02:45 PM Re: Help Javascript random generator
Novice Talker

Posts: 7
Trades: 0
Woaaa.....thanks for the help ^_^
@chrishirst ...:bowdeep 3X:

I'll try your solution first, never meant any disrespect (so please accept this apology and respect from this fool who don't really understand javascript and ajax)

c
ontinuing my 1st post here i upload the thing that i got...to explain the situation.

DOWNLOAD HERE


As u can see...on the javascript file (millquest.js) , i only manage to create a set....
which are:
stemarray[0] ='Monas adalah singkatan dari:'
answer[0] ='B'
adistract[0] ='Monumen Naskah'
bdistract[0] ='Monumen Nasional'
cdistract[0] ='Museum Nasional'
ddistract[0] ='Museum Naskah'

...all till number 7

But what i want to create are:

for question number 1-3 (which the contestant will win 100 point to Hadiah A), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 4-5 (which the contestant will win 100 point to Hadiah B), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 6 (which the contestant will win 100 point to Hadiah C), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 7 (which the contestant will win 100 point to Hadiah D), they consist of 75 questions that will be randomly displayed each time the quiz begin

for question number 8 (which the contestant will win 100 point to Hadiah E), they consist of 75 questions that will be randomly displayed each time the quiz begin

Thanks in advance...

best regards...

Last edited by mamamiaw; 08-04-2008 at 04:25 PM..
mamamiaw is offline
Reply With Quote
View Public Profile
 
Old 08-04-2008, 06:01 PM Re: Help Javascript random generator
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok so;
This is how I have read the schema.

75 random questions - no question asked twice in any session.

milestones are:-
question 3 - add 100 points if 1 to 3 are correct
question 5 - add 100 points if 4 & 5 are correct
question 6 - add 100 points if 6 is correct
question 7 - add 100 points if 7 is correct
question 8 - add 100 points if 8 is correct

does that sound correct?

and does a reset keep the data on which questions have been asked?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-04-2008, 10:58 PM Re: Help Javascript random generator
Novice Talker

Posts: 7
Trades: 0
Hi chrishirst

yes 75 random questions on point 1-3 with no question asked twice in any session.

another 75 on point 4 - 5, another 30 on point 6, another 30 on point 7 and another 50 on point 8 with no question asked twice in any session ^_^.

Yes it's correct, on every question answered (right answer) the contestand/student will get 100 points....

if question 3 answered - win Prize A (or equal to 100 points)
if question 5 answered - win Prize B (or equal to 100 points)
if question 6 answered - win Prize C (or equal to 100 points)
if question 7 answered - win Prize D (or equal to 100 points)
if question 8 answered - win Prize E (or equal to 100 points)

the prize is actually just some cookie or candy btw hehee...

and does a reset keep the data on which questions have been asked?

Nope...no storing and all....

Thanks for your kind reply ^_^

Best Regards

Last edited by mamamiaw; 08-06-2008 at 04:03 AM..
mamamiaw is offline
Reply With Quote
View Public Profile
 
Old 08-08-2008, 02:30 AM Re: Help Javascript random generator
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I'll be back playing with this shortly got sidetracked with some work.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-08-2008, 08:51 AM Re: Help Javascript random generator
Novice Talker

Posts: 7
Trades: 0
Quote:
Originally Posted by chrishirst View Post
I'll be back playing with this shortly got sidetracked with some work.
Thanks alot man ^_^
mamamiaw is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help Javascript random generator
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32845 seconds with 12 queries