/    Sign up×
Community /Pin to ProfileBookmark

Need help! Can this be done? Tricky JavaScript?!?!?!?

I was wondering if there is any way to do the following!

I need to have three text boxes, user will enter a number in each box.
Then i have four buttons, add, subtract, divide and multiply.
When i click on each button, the numbers in the first two text boxes should match the answer in the third text box. ex. (first box has a 4, second box has a 4, and the third box has an 8. if i click the add button, i should get a “correct” answer in the div below with a green background and if i hit the multiply button the div should say “wrong” with a red background.

Sorry but I’m a newbie to this and this is so confusing!?

This is what i have so far.
HTML

[CODE]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<script type=”text/javascript” src=”main.js”></script>
<title>Untitled Document</title>
<style type=”text/css”>
* {margin:0; padding:0;}
h1 {color:#666;}
#wrapper {margin:auto; padding:100px; width:960px;}
#resultdiv {
width:200px;
height:50px;
border:#FFFFFF 1px solid;
color:#FFFFFF;
}
</style>
</head>

<body style=”background-color:#000000″>
<div id=”wrapper”>
<h1>Javascript</h1>
<br />
<div>
<input id=”firstnum” type=”text” size=”20″ />
<input id=”secondnum” type=”text” size=”20″ />

</div>
<br />
<div>
<input onclick=”DoNums(1)” type=”button” name=”mybutton” value=”Add Numbers” />
<input onclick=”DoNums(2)” type=”button” name=”mybutton” value=”Subtract Numbers” />
<input onclick=”DoNums(3)” type=”button” name=”mybutton” value=”Multiply Numbers” />
<input onclick=”DoNums(4)” type=”button” name=”mybutton” value=”Divide Numbers” />
</div>
<br />
<div><input id=”thirdnum” type=”text” size=”20″ /></div>
<br />
<div id=”resultdiv”><p>RESULT HERE</p></div>

</div><!– end wrapper –>
</body>
</html>

[/CODE]

JavaScript

[CODE]
// JavaScript Document
// main.js

var right;
var wrong;

function DoNums (operations) {

var fnum = document.getElementById(“firstnum”).value;
var snum = document.getElementById(“secondnum”).value;
var tnum = document.getElementById(“thirdnum”).value;
var result = 0;
switch (operations)
{
case 1:result = parseInt (fnum) + parseInt (snum);
break;
case 2:result = parseInt (fnum) – parseInt (snum);
break;
case 3:result = parseInt (fnum) * parseInt (snum);
break;
case 4:result = parseInt (fnum) / parseInt (snum);
break;
default:alert(“invalid operation”);
break;
}

document.getElementById(“resultdiv”).innerHTML = result;

if (parseInt (tnum) == resultdiv)
{
document.getElementById(“resultdiv”).style.backgroundColor = green;
}
else
{
document.getElementById(“resultdiv”).style.backgroundColor = red;
}

}
[/CODE]

😮

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 28.2011 — I don't think you were too far off. ?

Major change was in the color designations of 'red' not red, etc.:eek:

I put JS inline for testing purposes only, you can move it back out if desired. ?

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

&lt;!-- script type="text/javascript" src="main.js"&gt;&lt;/script --&gt;
&lt;script type="text/javascript"&gt;
// JavaScript Document
// main.js

<i> </i>var right;
<i> </i>var wrong;

function DoNums (operations) {
var fnum = document.getElementById("firstnum").value;
var snum = document.getElementById("secondnum").value;
var tnum = document.getElementById("thirdnum").value;
var result = 0;
switch (operations) {
case 1:result = parseInt (fnum) + parseInt (snum); break;
case 2:result = parseInt (fnum) - parseInt (snum); break;
case 3:result = parseInt (fnum) * parseInt (snum); break;
case 4:result = parseInt (fnum) / parseInt (snum); break;
default:alert("invalid operation"); break;
}
var elem = document.getElementById("mathResult");
elem.innerHTML = result;
if (parseInt(tnum) == result) { elem.style.backgroundColor = 'green'; }
else { elem.style.backgroundColor = 'red'; }

}
&lt;/script&gt;

&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
* {margin:0; padding:0;}
h1 {color:#666;}
#wrapper {margin:auto; padding:100px; width:960px;}
#mathResult {
width:200px;
height:50px;
border:#FFFFFF 1px solid;
color:#FFFFFF;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body style="background-color:#888"&gt;
&lt;div id="wrapper"&gt;
&lt;h1&gt;Javascript&lt;/h1&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;input id="firstnum" type="text" size="10" /&gt;
&lt;input id="secondnum" type="text" size="10" /&gt; =
&lt;input id="thirdnum" type="text" size="10" /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;input onclick="DoNums(1)" type="button" name="mybutton" value="Add Numbers" /&gt;
&lt;input onclick="DoNums(2)" type="button" name="mybutton" value="Subtract Numbers" /&gt;
&lt;br&gt;
&lt;input onclick="DoNums(3)" type="button" name="mybutton" value="Multiply Numbers" /&gt;
&lt;input onclick="DoNums(4)" type="button" name="mybutton" value="Divide Numbers" /&gt;
&lt;/div&gt;
&lt;p /&gt;
&lt;div id="mathResult"&gt;&lt;p&gt;RESULT HERE&lt;/p&gt;&lt;/div&gt;

&lt;/div&gt;&lt;!-- end wrapper --&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@unstable_nesauthorJul 29.2011 — Thanks buddy!!! Appreciate you!!!
Copy linkTweet thisAlerts:
@JMRKERJul 29.2011 — Thanks buddy!!! Appreciate you!!![/QUOTE]

You're most welcome.

Happy to help.

Good Lulck!

?
×

Success!

Help @unstable_nes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...