/    Sign up×
Community /Pin to ProfileBookmark

Help me with my calculator pls

Hi guys. What am trying to do is to create a a simple calculator. The page will contain 3 textbox. First is the textbox for the 1st number, second textbox is for the second number and the third box is the where the user could input an arithmetic operator like + / * – …. I’m planning to do this as a switch apparently im having problems with it. Pls help guys … Here’s my code so far

[QUOTE]

<HEAD>
<title>Button</title>
<style type=”text/css”>
body
{
background-color:#B8BFD8;
}
</style>

<SCRIPT LANGUAGE=”JavaScript”>

switch(ans) {
case “+”:
document.write(“Answer: ” + ans)
break;

case “-“:
document.write(“Answer: ” + ans)
break;

case “*”:
document.write(“Answer: ” + ans)

case “/”:
document.write(“asnwer: ” + ans)

default:
document.write(“Not Applicable”)

}

</SCRIPT>

<BODY>

Enter first number:
<input type=number size=4 value=” ” name=”a”>

Enter second number:
<input type=number size=4 name=”b”>

Enter operator:
<input type=number size=4 name=”ans”>

</body>
</html>

[/QUOTE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJul 26.2010 — 1) you need to put the 'switch', that does the calculation, into a function. You also need to add code to actually do the calculation.

2) markup a button labeled '=' or something similar.

3) attach an onclick event handler to the button in 2) that runs the function in 1) that does the caluculation and displays the result.

I wouldn't use document.write(). My suggestion would be to display the calc. result in a 'readonly' element of your choice.

Another way of building your calculator you might like to consider is:

1) have 1 textbox that will containg the expression to evaluate - example: 3+2

2) set up a number pad and operator buttons similar to a real calculator with each number button having an onclick event handler that passes the value of that button to a function that concatenates the value of that button to a string it is building.

3) set up operator buttons that operate similarly to the buttons in 2) except that these button pass the operator symbol.

The onclick function concatenates the value passed to it to a string that is set to the value of the textbox in 1.

As the user is clicking away, the string in the textbox is gradually being built to something like 4+2

When the user clicks the equals button in the calculator, a function then uses eval() to evaluate the string in the textbox and displays the result in the same textbox.
×

Success!

Help @jomawie 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...