Jump to content

Build Theme!
  •  
  • Infected?

WE'RE SURE THAT YOU'LL LOVE US!

Hey there! :wub: Looks like you're enjoying the discussion, but you're not signed up for an account. When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever new posts are made. You can like posts to share the love. :D Join 91983 other members! Anybody can ask, anybody can answer. Consistently helpful members may be invited to become staff. Here's how it works. Virus cleanup? Start here -> Malware Removal Forum.

Try What the Tech -- It's free!


Photo

Looping


  • Please log in to reply
2 replies to this topic

#1 GaryORLFC

GaryORLFC

    Authentic Member

  • Authentic Member
  • PipPip
  • 56 posts
  • Interests:Sports, Music, Gaming, Computers & Drinks With Friends.

Posted 11 October 2010 - 05:42 AM

Hey there!

I have come accross a problem in using the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Addition</title>
<script type="text/javascript">



var num1,
num2,
answer,
correctAnswer;

num1 = Math.round(Math.random()*20);
num2 = Math.round(Math.random()*20);
correctAnswer = num1 + num2;

function getAnswer(){
var value = parseInt(document.form.number.value);
if(comparingAnswer(value) == 1){
document.writeln("Correct! Well Done." + num1 + " + " + num2 + " = " + correctAnswer);
}
else{
document.writeln("Incorrect." + num1 + " + " + num2 + " = " + correctAnswer);
}
}

function comparingAnswer(answer){
if (answer == correctAnswer){
return 1;
}
else{
return 0;
}
}

document.writeln("<form name=\"form\">");
document.writeln("<table>");
document.writeln("<tr><td>" + num1 + " + " + num2 + " = " + "</td><td><input style = \"font-size:200%\" name = \"number\" type = \"text\" /></td></tr>");
document.writeln("</table>");
document.writeln("</form>");


</script>
</head>
<body onkeydown = "if(event.keyCode==13){document.getElementById(getAnswer()).click();}"
</body>
</html>


The code works fine, but what I am trying to do with this code is make it work as a loop.

What I want is when the user hits enter (answers the question) it will take you to another question (20 questions in total). Then after that, it will display what questions you got wrong and what questions you got right. When I tried to loop it before, it just gave me two questions instead of one and it wouldn't let you know if you were right or wrong. The loop never ended.

Plus, I want to make the text bigger. The text box I can make bigger no problem , but the actual sum I can only make it as big as +6.

Could someone please help me with this?

Posted Image

    Advertisements

Register to Remove


#2 terry1966

terry1966

    SuperMember

  • Visiting Tech
  • PipPipPipPipPip
  • 2,988 posts

Posted 12 October 2010 - 08:30 AM

it's been many years(20+) since i've done any real programming, and never with javascript, so i won't be able to give you any working code but here's a link to the type of loop i'd probably use called a while loop :- http://www.w3schools..._loop_while.asp i hope this will be of some help to you until someone else with better knowledge/experience has the time to reply..

:popcorn:

#3 Overbooked

Overbooked

    Silver Member

  • Authentic Member
  • PipPipPip
  • 347 posts

Posted 21 October 2010 - 02:43 AM

Hi GaryORLFC,

Just like terry1966, I'm no Javascript expert, but the biggest hurdle I see is how to retain the data between questions. If you want to be able to show what has been answered previously there should be a way for you to store the information each time a question is answered. Just to give you an example, I've revised your code a little bit just to get the loop to work properly:

JScript:

<script type="text/javascript">
var num1, num2, answer, correctAnswer;

function getAnswer() {
	var value = parseInt(document.getElementById('answer').value);
	var lc = parseInt(document.getElementById('loopNumber').value);
	
	if(compareAnswer(value) == 1) {
		document.getElementById('message').value = "Correct! It's " + correctAnswer + " Loop " + lc;
	}
	else {
		document.getElementById('message').value = "Wrong! It's " + correctAnswer + " Loop " + lc;
	}
	
	lc++;
	document.getElementById('loopNumber').value = lc;
	
	if(lc < 20) {
		loadForm();
		document.getElementById('answer').value = "";
	}
	else {
		document.getElementById('message').value = "No more";
	}
}

function compareAnswer(answer) {
	if(answer == correctAnswer) {
		return 1;
	}
	else {
		return 0;
	}
}

function loadForm() {
	num1 = Math.round(Math.random()*20);
	num2 = Math.round(Math.random()*20);
	correctAnswer = num1 + num2;
	document.getElementById('num1').value = num1;
	document.getElementById('num2').value = num2;
}

loadForm();

</script>

Body:

<body onLoad="loadForm()">
<form action="hp.html" method="get">
<table cellpadding="0" cellspacing="0" border="0" width=150px>
<tr>
<td><input type="text" name="num1" size="1" maxlength="2" id="num1"></td><td>+</td><td><input type="text" name="num2" size="1" maxlength="2" id="num2"></td><td><button name="compute" type="button" onClick="getAnswer()">Compute</button></td>
</tr>
<tr>
<td><input type="text" name="answer" size="1" maxlength="2" id="answer"></td><td>x</td><td><input type="hidden" name="loopNumber" value="0" id="loopNumber"></td><td><input type="text" name="message" size="20" maxlength="50" id="message"></td>
</tr>
</table>
</form>
</body>

I have to apologize for mutilating the code you provided but I have never written Javascript before. I'm sure there more efficient ways than the one I came up with :lol:

I used a hidden form field to temporarily store the loop counter (<input type="hidden" name="loopNumber" value="0" id="loopNumber">) so we can retrieve and increment it each time the user clicks the button to check their answer. Using cookies may be something to look into or better yet server-side scripting.

HTH.
Thank you, Overbooked

Related Topics



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users