Jump to content

Recommended Posts

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 post
Share on other sites

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 by JMRKER
Link to post
Share on other sites

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 by JMRKER
Trying to point out problems to fix going forward.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...