1 Replies - 1713 Views - Last Post: 26 November 2012 - 11:01 PM

#1 techhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 01-June 12

Displaying score for quiz

Posted 26 November 2012 - 10:25 PM

I am having troubles displaying the score and the page won't load immediately could someone please have a look at it and check the codes if I done it right, Thanks!-

<!DOCTYPE html>
<html>
<body>

<p>Quiz</p>

<script type = "text/javascript"">
var score = 0;

var value = prompt("How many seconds in one hour: ", "");

	if (value == "60")
	{
		alert("Yes correct!");
	score += 1; //add 1 to score
	}
	else
	{
		alert("Sorry the answer is incorrect, the answer is 60");
	}


var value = prompt("How many days in one week: ", "");

	if (value == "7")
	{
		alert("Yes correct!");
	score += 1;
	}
	else
	{
		alert("Sorry the answer is incorrect, the answer is 7");
	}

var value = prompt("How many days in one year: ", "");

	if (value == "365")
	{
		alert("Yes correct!");
	score += 1;
	}
	else
	{
		alert("Sorry the answer is incorrect, the answer is 365");
	}

document.write("Your score is  (score)  out of 3");

</script>

</body>
</html>

This post has been edited by Dormilich: 26 November 2012 - 11:45 PM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: Displaying score for quiz

#2 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Displaying score for quiz

Posted 26 November 2012 - 11:01 PM

Have you tried using a syntax highlighter such as notepad++? Copy/pasting your code into np++ showed one too many quotes being used towards the beginning of your code.

Also, on line 47:
document.write("Your score is (score) out of 3");



Kind of looks like (score) should be the variable score, and not in a string. You might want something like this:
document.write("Your score is" + score + "out of 3");




As for the page not seeming to load immediately, all of your javascript is being executed before the entire page loads, and until the javascript is done(ie until the prompts have been filled out), the page will not entirely load.


EDIT: In the future, try to avoid using document.write. What happens when you run this code and press the button?
<!DOCTYPE html>
<html>
	<head>
		<script type = "text/javascript">
			function writeToPage() {
				document.write("<h1>Test</h1>");
			}
		</script>
	</head>
	<body>
	<h1>Original Text</h1>
	<button onclick="writeToPage()">Click Me!</button>
	</body>
</html>


Further reading

This post has been edited by Sho Ke: 26 November 2012 - 11:06 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1