Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can you help me at calculator java script?

    <!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>Untitled Document</title>
    <script type="text/javascript">

    //variables
    var firstValue=0;
    var secondValue=0;
    var result=0;

    addClick=false;
    minusClick=false;

    function numberButtonPressed(number){
    document.form1.inputValue.value+=number;
    }

    function addButtonPressed(){
    firstValue=parseInt(document.getElementById("inputValue").value);
    document.form1.inputValue.value="";
    addClick=true;
    minusClick=false;
    }

    function minusButtonPressed()
    {
    firstValue=parseInt(document.getElementById("inputValue").value);
    document.form1.inputValue.value="";
    addClick=false;
    minusClick=true;
    }
    function equalButtonPressed(){
    secondValue=parseInt(document.getElementById("inputValue").value);
    document.form1.inputValue.value="";

    if (addClick==true){
    result=firstValue+secondValue;
    document.getElementById("inputValue").value=result;
    }
    else if (minusClick==true){
    result=firstValue-secondValue;
    document.getElementById("inputValue").value=result;
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="" action="">
    <input id = "inputValue" type = "text" size = "40" />
    <p> <input name="button4" type = "button"
    onclick = "numberButtonPressed(7)" value = "7" />
    <input name="button5" type = "button"
    onclick = "numberButtonPressed(8)" value = "8" />
    <input name="button6" type = "button"
    onclick = "numberButtonPressed(9)" value = "9" />
    <input name="button12" type = "button"
    onclick = "addButtonPressed()" value = "+" />
    <p>
    <input name="button" type = "button"
    onclick = "numberButtonPressed(4)" value = "4" />
    <input name="button2" type = "button"
    onclick = "numberButtonPressed(5)" value = "5" />
    <input name="button9" type = "button"
    onclick = "numberButtonPressed(6)" value = "6" />
    <input name="button13" type = "button"
    onclick = "minusButtonPressed()" value = "-" />
    <p>
    <input name="button3" type = "button"
    onclick = "numberButtonPressed(1)" value = "1" />
    <input name="button7" type = "button"
    onclick = "numberButtonPressed(2)" value = "2" />
    <input name="button8" type = "button"
    onclick = "numberButtonPressed(3)" value = "3" />
    <input name="button10" type = "button"
    onclick = "numberButtonPressed(0)" value = "0" />
    <input name="button14" type = "button"
    onclick = "equalButtonPressed()" value = "=" />

    </form>
    </body>
    </html>




    This calculator normally runs.For example; I entered number. Then I clicked "+" or "-", I entered another number and finally I click "=". This is no problem.

    But like this, I entered number. Then I clicked "+" and "-" , I entered another number and finally I click "=". Give error. Write "NaN"


    I want to be current last clicked..

    Please help me..Thank you in advance..

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I think this fixes it!

    Code:
    function addButtonPressed(){
    firstValue=parseInt(document.getElementById("inputValue").value);
    if (isNaN(firstValue)) { firstValue = fv2}
    fv1 = firstValue;
    document.form1.inputValue.value="";
    addClick=true;
    minusClick=false;
    }
    
    function minusButtonPressed() {
    firstValue=parseInt(document.getElementById("inputValue").value);
    if (isNaN(firstValue)) { firstValue = fv1}
    fv2 = firstValue;
    document.form1.inputValue.value=""; 
    addClick=false; 
    minusClick=true;
    }

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Really, thank you very much.
    problem is solved..

  • #4
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <!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>Untitled Document</title>
    <script type="text/javascript">

    //variables
    var firstValue=0;
    var secondValue=0;
    var result=0;

    var addClick;
    var minusClick;

    function numberButtonPressed(number)
    {
    document.form1.inputValue.value+=number;
    }

    function buttonPressed(fonkClick)
    {
    firstValue=parseInt(document.getElementById("inputValue").value);
    document.form1.inputValue.value="";
    minusClick=false;
    addClick=false;

    fonkClick=true;
    }

    function equalButtonPressed()
    {
    secondValue=parseInt(document.getElementById("inputValue").value);
    document.form1.inputValue.value="";
    if (addClick==true)
    {
    result=firstValue+secondValue;
    document.getElementById("inputValue").value=result;
    }
    else if (minusClick==true)
    {
    result=firstValue-secondValue;
    document.getElementById("inputValue").value=result;
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="" action="">
    <input id = "inputValue" type = "text" size = "40" />
    <p> <input name="button4" type = "button"
    onclick = "numberButtonPressed(7)" value = "7" />
    <input name="button5" type = "button"
    onclick = "numberButtonPressed(8)" value = "8" />
    <input name="button6" type = "button"
    onclick = "numberButtonPressed(9)" value = "9" />
    <input name="button12" type = "button"
    onclick = "buttonPressed(addClick)" value = "+" />
    <p>
    <input name="button" type = "button"
    onclick = "numberButtonPressed(4)" value = "4" />
    <input name="button2" type = "button"
    onclick = "numberButtonPressed(5)" value = "5" />
    <input name="button9" type = "button"
    onclick = "numberButtonPressed(6)" value = "6" />
    <input name="button13" type = "button"
    onclick = "buttonPressed(minusClick)" value = "-" />
    <p>
    <input name="button3" type = "button"
    onclick = "numberButtonPressed(1)" value = "1" />
    <input name="button7" type = "button"
    onclick = "numberButtonPressed(2)" value = "2" />
    <input name="button8" type = "button"
    onclick = "numberButtonPressed(3)" value = "3" />
    <input name="button10" type = "button"
    onclick = "numberButtonPressed(0)" value = "0" />
    <input name="button14" type = "button"
    onclick = "equalButtonPressed()" value = "=" />

    </form>
    </body>
    </html>


    This program doesn't calculate the result.
    Can you tell me problem here.
    Thankyou in advance

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I thought you said that your problem was solved?

    For simple typos etc. use your error console. What is fonkclick supposed to be?

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •