Ayman_Khoshouey Posted July 11, 2020 Share Posted July 11, 2020 what is error of this? It give me wrong results <html> <head> <title>Calculator</title> <script language='JavaScript'> var number='0'; var start=0; var firstNumber=0; var result=0; var sign=''; calculator.results.value='0' function show(number,sign){ if (sign !=''){ calculator.results.value=''; calculator.results.value=number; } else{ calculator.results.value=calculator.results.value+number; } //start++ } function add(){ sign='+'; firstNumber=parseFloat(calculator.results.value); calculator.results.value=''; } function subtract(){ sign='-'; firstNumber=parseFloat(calculator.results.value); calculator.results.value=''; } function equal(sign){ if (sign='+'){ result=parseFloat(calculator.results.value)+firstNumber; calculator.results.value=result; } if (sign='-'){ result=firstNumber-parseFloat(calculator.results.value); calculator.results.value=result; } } </script> </head> <body> <form name="calculator"> Calculator <br/> <input type="text" name="results" value="" /> <br/> <br/> <input type="button" value="1" onclick="show('1','')" /> <input type="button" value="2" onclick="show('2','')" /> <input type="button" value="3" onclick="show('3','')" /> <br/> <input type="button" value="4" onclick="show('4','')" /> <input type="button" value="5" onclick="show('5','')" /> <input type="button" value="6" onclick="show('6','')" /> <br/> <input type="button" value="7" onclick="show('7','')" /> <input type="button" value="8" onclick="show('8','')" /> <input type="button" value="9" onclick="show('9','')" /> <br/> <input type="button" value="+" onclick="add()" /> <input type="button" value="-" onclick="subtract()" /> <input type="button" value="=" onclick="equal(sign)" /> <br/> </form> </body> </html> Link to comment Share on other sites More sharing options...
JMRKER Posted July 14, 2020 Share Posted July 14, 2020 (edited) How OLD is that script you found? <script> no longer requires the language='JavaScript' addition. Notice that you clear out the 'results.value' and change the 'firstNumber' value as soon as you press '+' or '-' Then when you press the '=' key, the blank value is added to the newest entry. You may want to re-think your logic here. BTW, welcome to the forums. Edited July 14, 2020 by JMRKER Link to comment Share on other sites More sharing options...
JMRKER Posted July 15, 2020 Share Posted July 15, 2020 (edited) Also look at your '=' key logic where you check for the previous '+' or '-' key entry. I don't think you want to CHECK for the status of the key with this: if (sign='+'){ Other thoughts: 1. What kind of calculator does not let you enter a zero? 2. And why do you need to use parseFloat() when you have not ability to enter a decimal? Edited July 15, 2020 by JMRKER Trying to point out problems to fix going forward. 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