Jump to content

Javasript help

- - - - -

  • Please log in to reply
1 reply to this topic

#1
bobbysto

bobbysto

    Newbie

  • Members
  • Pip
  • 1 posts
Hey for assignment in college we have to make a quiz in our website, my current code for the javascript that is exercuted inthe <head> tag is...
<script type="text/javascript">

var score = 0;

var questions = [

['What do Piggy Banks hold?', 'money'],

['What sign represents pounds?', '£'],

['What sign represents dollars', '$']

];

function askQuestion(question) {

	var anwser = prompt(question[0],'');

		if (anwser == question[1]) {

		alert('Yes! That’s right');

		score++;

		} else {

		alert('Sorry :( That seems to be wrong, the answer was ' + question[1]);

		}

}

for (var i=0; i<questions.length; i++) {

askQuestion(questions[i]);

}

</script>

Then in one of my div's i exercuted the following code...
<script type="text/javascript">

var message = 'You got ' + score;

message += ' out of ' + questions.length;

message += ' questions correct.';

document.write(message);

</script>

My problem is the page doesnt display as the quiz exercutes, because its in the head tag, so how could i format my code so the page loads then the javascript loads, i tried putting the code in the head moved to the footer but then the scoring display is already exercuted.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Put the code in a function, and then you can do
<body onload="functionName()">





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users