Reply
Memory function for a calculator
Old 12-01-2006, 10:27 AM Memory function for a calculator
Experienced Talker

Posts: 37
Right so im trying to make a memory function on a calculator, i want it to take the value in the input field and add it to a variable.

but i cant get it 2 do that *compleate memory blank*

all i have managed to do, is get it to display the variable, and not add to it.

so far i have...

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Javascript Calculator</title>
<script language="JavaScript">
<!-- Begin

var memory = 0

function memoryshow(input) {
    input.value = memory
}

// if the number is 0, the new character will replace it, if it is not 0 then it will add it to the form.
function addChar(input, character) {
    if(input.value == null || input.value == "0")
        input.value = character
    else
        input.value += character
}

//Deletes the last digit in the form field.
function deleteChar(input) {
    input.value = input.value.substring(0, input.value.length - 1)
}

//Calculates the equation in the input field.
function compute(form)  {
    form.display.value = eval(form.display.value)
}

//Checks to see if the numbers/characters added into the input field are numbers or mathmatical symbals.
function checkNum(str)  {
    for (var i = 0; i < str.length; i++) {
    var ch = str.substring(i, i+1)
    if (ch < "0" || ch > "9") {
    if (ch != "/" && ch != "*" && ch != "+" && ch !="-" && ch != ".") {
        alert("Invalid Entry!")
    return false
      }
    }
    }
    return true
}
// End -->
</script>
</head>

<body>
    <form>
        <input name="display" value="0" size="15">
        <br />
        <input type="button" value=" 7 " onClick="addChar(this.form.display, '7')">
        <input type="button" value=" 8 " onClick="addChar(this.form.display, '8')">
        <input type="button" value=" 9 " onClick="addChar(this.form.display, '9')">
        <input type="button" value=" / " onClick="addChar(this.form.display, '/')">
        <br />
        <input type="button" value=" 4 " onClick="addChar(this.form.display, '4')">
        <input type="button" value=" 5 " onClick="addChar(this.form.display, '5')">
        <input type="button" value=" 6 " onClick="addChar(this.form.display, '6')">
        <input type="button" value=" * " onClick="addChar(this.form.display, '*')">
        <br />
        <input type="button" value=" 1 " onClick="addChar(this.form.display, '1')">
        <input type="button" value=" 2 " onClick="addChar(this.form.display, '2')">
        <input type="button" value=" 3 " onClick="addChar(this.form.display, '3')">
        <input type="button" value=" - " onClick="addChar(this.form.display, '-')">
        <br />
        <input type="button" value=" 0 " onClick="addChar(this.form.display, '0')">
        <input type="button" value=" .  " onClick="addChar(this.form.display, '.')">
        
        <input type="button" value="=" name="enter" onClick="if (checkNum(this.form.display.value)) { compute(this.form) }">
        <input type="button" value=" + " onClick="addChar(this.form.display, '+')">
        <br />
        <br />
        <input type="button" value="Clear" onClick="this.form.display.value = 0 ">
        <input type="button" value="Delete" onClick="deleteChar(this.form.display)">
        <input type="button" value=" M " onclick="memoryshow(this.form.display)" />
    </form>
</body>
</html>
any help would be much appriciated.
gaz545 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Reply     « Reply to Memory function for a calculator
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.10746 seconds with 12 queries