Jump to content

calculator


Matej

Recommended Posts

the problem is likely that you are getting the reference to lol before the page has completely loaded. You should put code like that in an onload event.

http://jsfiddle.net/7D9gR/2/

Link to comment
Share on other sites

why would you move it out of the function and inline? That is a step backwards from what you had

Link to comment
Share on other sites

I've played around with your fiddle a bit and this is what I came up with...

 

http://jsfiddle.net/7D9gR/8/

 

I made its style appear much like a regular keypad and gave it similar behavior of how a basic calculator displays the input and output. I haven't put it through rigorous testing however.

Link to comment
Share on other sites

also i would like to do the thing , that you can use "-" only twice in a row , but

 function ahoj(plot){if( x.value.lastIndexOf(plot)!=x.value.length-2){    x.value+=plot;}} 

any hint? :)

Edited by Matej
Link to comment
Share on other sites

The biggest hint I could say is you heading in the wrong direction. Try to move away from using inline coding (placing javascript inside the html elements). Also never, EVER use eval unless you have to, and you don't have to here.

 

If you intend on making a proper calculator don't "directly" rely on the values taken from the elements to store the numbers for calculation. Remember that HTML is mostly meant for presentation; grab the values, convert them to proper numbers, and place them inside variables for you to mess with on javascript's side. You'll have a much easier time coding the logic if you're not constantly dancing around string manipulation.

 

Similar to what I do in my version... in the "calculate" function, I pull the numbers from the "screen" and put them into the "left" and "right" variables which exist only in javascript's side and not in the html. I have Javascript mess with all the math on it's end. Once the calculations have been solved I then have javascript return the answer back to the screen. Of course, my code is a little bit more involved than that, but that is the basic procedure you should be grasping from what it's doing.

 

My code focuses more on the fact that equations are pretty much made up of 2 things: Operators [+, -, *, /] and Operands [0-9, .]; and it first recognizes those 2 building blocks and then works to combine them to actually build and solve an equation. I try to think up a programs building blocks 1st and then start to code from there. Its a mindset I think you should get into if you plan to start coding as well, especially since its integral to how OOP works. When it comes to making applications, even a simple calculator, having a OOP mindset will really help you both visualize and program out your creations.

 

That being said if you want to add support for negative numbers, even though I said in the fiddle that mine lacked any support, I would simply create an extra button that had "(-)" or "+/-" on it to denote that its meant to denote flipping the polarity of the current number. this button isn't technically an operator nor operand, just a button that flips the polarity of the rightmost operand

 

I guess thats my hint...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...