
Function inside math function
Ok, so I'm sure this has a simple solution, but I can't work it out and it's really bugging me!
I've got my code, and the task is to generate two random numbers, the user then inputs an answer for them added together, then the program checks the answer and displays either "correct" or "wrong"
Here's some of my code:
Code:
<HTML>
<TITLE>Assessment Task 3 : Rohan Gardiner</TITLE>
<HEAD>
<SCRIPT LANGUAGE ="JavaScript">
function maths()
{
var response;
var answer;
answer = document.questions.answer.value;
if (answer==document.adding)
{
response = "correct";
}
else
{
response = "wrong";
}
document.questions.result.value = response ;
}
function randoms()
{
rndNum = Math.random();
Num = rndNum*20;
Num1=rndNum*10
document.write(Math.round(Num)+"+"+ Math.round(Num1));
}
function adding()
{
document.write(Math.round(Num) + Math.round(Num1));
}
</SCRIPT>
</HEAD>
<BODY>
<h1 align="center">Rohan Gardiner Assessment Task 3</h1>
<FORM NAME = "questions">
<SCRIPT Language=JavaScript> randoms(); </script>
=
<INPUT TYPE = "textbox" NAME = "answer" > <BR>
<INPUT NAME = "dobutton" TYPE = "button" Value = "check" onClick= "maths()">
<INPUT TYPE = "textbox" NAME = "result" >
</BODY>
</HTML>
Yes, I am a noobie coder. Please help

In future, I'd suggest you say what your problem is as well as what your code does!
But, the first problem I see lies here:
Code:
if (answer==document.adding)
That doesn't make sense. A better layout would possibly be something along these lines.
Code:
function maths()
{
var response;
var answer;
answer = document.questions.answer.value;
if (Number(answer)==sum)
{
response = "correct";
}
else
{
response = "wrong";
}
document.questions.result.value = response ;
}
function randoms()
{
rndNum = Math.random();
Num = Math.round(rndNum*20);
Num1=Math.round(rndNum*10);
sum = Num+Num1;
document.write(Num+"+"+ Num1);
}
function adding()
{
document.write(sum);
}
Unchecked, so I'm unsure if it works.
BTW, do you want it always to be a pair like, 20+10,19+9,18+9,17+8 etc.? If not, you need two random numbers.
Also, I would advise that you get into the habit NOW of indenting your code correctly, or learn to use an editor that will do so for you. It will help! And in the future, you won't want to be using implied global variables like you are now.
Great wit and madness are near allied, and fine a line their bounds divide.

Works
Works beautifully.
Thankyou, sir!

Simple Math Equation Help Needed
I have two, dollar values (a and b). I need to divide the larger (max) into the smaller (min) and multiply by 100 to get the percentage difference (c value) for box3.
The following javascript is what I wrote into the Acrobat PDF (under calculations), but I get NaN instead of a number.
I've tried multiple variations, and tried researching this relatively simple equation to find code, but without finding or understanding what I need. I'm not a programmer and need help.
This is my best attempt:
var a=this.getField("Box1");
var b=this.getField("Box2");
var c=this.getField("Box3");
var d=Math.min(a,b);
var e=Math.max(a,b);
c.value=(e.value/d.value)*100;
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

