Matej Posted July 29, 2014 Share Posted July 29, 2014 (edited) Hi . im trying to do a calculator but this function does now work with error "Cannot set property 'value' of null " while i deffinet it earlier... http://jsfiddle.net/7D9gR/ can someone help< Edited July 29, 2014 by Matej Link to comment Share on other sites More sharing options...
Matej Posted July 29, 2014 Author Share Posted July 29, 2014 even when i dont use this function like here http://jsfiddle.net/7D9gR/1/ , the value isnt "updating" , for example when value is 7 and i click on 8 , it doesnt make 78 but 8 replaces 7 Link to comment Share on other sites More sharing options...
thescientist Posted July 29, 2014 Share Posted July 29, 2014 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 More sharing options...
thescientist Posted July 29, 2014 Share Posted July 29, 2014 regarding that issue, it's because you are not concatentating the values together correctly. use += http://jsfiddle.net/7D9gR/3/ Link to comment Share on other sites More sharing options...
Matej Posted July 29, 2014 Author Share Posted July 29, 2014 (edited) thanks , did it like this http://jsfiddle.net/7D9gR/4/ // the .value always returns string right? Edited July 29, 2014 by Matej Link to comment Share on other sites More sharing options...
thescientist Posted July 29, 2014 Share Posted July 29, 2014 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 More sharing options...
Hadien Posted July 29, 2014 Share Posted July 29, 2014 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 More sharing options...
Matej Posted July 30, 2014 Author Share Posted July 30, 2014 (edited) well , did it like this http://jsfiddle.net/7D9gR/7/ // Haidien your fiddle is interessting , but im not very good at OOP yet Edited July 30, 2014 by Matej Link to comment Share on other sites More sharing options...
Matej Posted July 30, 2014 Author Share Posted July 30, 2014 (edited) 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 July 30, 2014 by Matej Link to comment Share on other sites More sharing options...
Hadien Posted July 30, 2014 Share Posted July 30, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now