简体   繁体   中英

Javascript Calculator Memory function

Here is my HTML

    <script type="text/javascript" src="./The Desktop Calculator_files/calc.js"></script>
    <style type="text/css"></style>
</head>

<body onLoad="checkBrowser()">
<form id="calcForm">

<div id="calc">
<input type='hidden' id='param1' value='0' />
<input type='hidden' id='operator' value='' />
    <div id="display">
        <input type="text" name="disp" id="disp" class="disp" size="36" value="0">
    </div>
    <div id="buttons">
        <div class="row">
            <input type="button" value="7" onclick="isNum();appendMe(this.value)">
            <input type="button" value="8" onclick="isNum();appendMe(this.value)">
            <input type="button" value="9" onclick="isNum();appendMe(this.value)">
            <input type="button" value="/" onClick="isNum();setOp(this.value)">
            <input type="button" value="CE">
        </div>
        <div class="row">
            <input type="button" value="4" onclick="isNum();appendMe(this.value)">
            <input type="button" value="5" onclick="isNum();appendMe(this.value)">
            <input type="button" value="6" onclick="isNum();appendMe(this.value)">
            <input type="button" value="*" onClick="isNum();setOp(this.value)">
            <input type="button" value="C" onclick="clearAll()">
        </div>
        <div class="row">
            <input type="button" value="1" onclick="isNum();appendMe(this.value)">
            <input type="button" value="2" onclick="isNum();appendMe(this.value)">
            <input type="button" value="3" onclick="isNum();appendMe(this.value)">
            <input type="button" value="-" onClick="isNum();setOp(this.value)">
            <input type="button" value="M" onClick="isNum();set_getMem()">
        </div>
        <div class="row">
            <input type="button" value="0" onclick="isNum();appendMe(this.value)">
            <input type="button" value="+/-" onclick="isNum();plusMinus()">
            <input type="button" value="." onclick="isNum();appendMe(this.value)">
            <input type="button" value="+" onClick="isNum();setOp(this.value)">
            <input type="button" value="=" onClick="isNum();calcMe()">
        </div>
        </div>      
<div id='warning'>Your Browser Can't Handle The Truth!</div> 
</div>
</form>


</body></html>

Here is my JavaScript

function appendMe(val)
{
    //alert(val);
    //document.getElementById("disp").value+=val;
    //alert(val);
if(document.getElementById("disp").value=='0')
{
  document.getElementById("disp").value=val;
}
else if(val=='.' && document.getElementById("disp").value.indexOf('.')>-1) //do nothing, because we already have a decimal point
{

}
else //in any other case, we just append
{
  document.getElementById("disp").value+=val;
}
    }
function clearAll()
{
    //alert(val);
    document.getElementById("disp").value=0;
}

function checkBrowser()
{
    alert("checking");
    document.getElementById("warning").style.display="none";
}

function plusMinus()
{
    document.getElementById("disp").value=(document.getElementById("disp").value*-1);
}
function setOp(val)
{
//first, set aside the initial value as entered
document.getElementById("param1").value=document.getElementById("disp").value;

//next, clear out that first number entered
document.getElementById("disp").value=0;

//finally, store the operation
document.getElementById("operator").value=val;
} 
function calcMe()
{
var param1 = document.getElementById("param1").value;
var operator = document.getElementById("operator").value;
var param2 = document.getElementById("disp").value;

document.getElementById("disp").value = eval(param1+operator+param2); 
}

function isNum()
{
//start as true
var isN = true;

if(isNaN(document.getElementById("disp").value))
{
isN=false;
alert("Non-numeric Data!");
}

return isN;
}


function set_getMem()
{
var memvalue;
//{
//isNum()
//}
if(memvalue == null ) //nothing in there, so set it
    {
    memvalue = document.getElementById("disp").value;
    }

else //something in there, so display it
    {
    document.getElementById("disp").value = memvalue;
    }
}

The part I am having problems with is getting the M button to function properly. What I want to happen is that I can click M and it will save whatever is in the display except when there is already a number stored I want it to display that number. Currently I click the M button and it doesn't appear to save a number or display a number.

Edited: Based on feedback I got the Memory function to work but now I need a function that can clear the value of the global variable.

function clear_All()
{
var memvalue=0;
document.getElementById("disp").value=0;
var param1=0;
var param2=0;

}

When I put the memvalue to 0 in the function it doesnt clear it from memvalue. When I put it outside the function it just breaks the storing capabilities of the memvalue.

Here might be the problem:

function set_getMem()
{
  var memvalue;

You define memvalue as a local variable inside set_memGet() , therefore this variable is gone once the function returns.

Define this variable out of the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM