Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Calculator

  1. #1
    New Coder
    Join Date
    Aug 2008
    Thanked 0 Times in 0 Posts


    Hello I wish to make a basic calculator in javascript for my fitness website.

    There are three sections to the page, an input box, a submit button and a paragraph where i would like the result to come out.

    I would like to take the figure inputed, and put it in this equation:
    111.33 - (0.42 x Inputted Figure)
    Then make it appear in the paragraph tag when the submit button is clicked.

    I know very very basic javascript but I'm just trying to figure out where I should start, and how to structure it.
    I would mucho appreciate any pointers

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    Here you are:-

    <form name = "myform">
    Enter a value <input type = "text" name = "nput1">
    <input type = "button" name= "but1" value = "Calculate Result" onclick = "calc()">
    Result <input type = "text" name = "result" readonly=readonly><br><br>
    <p id = "rlt"></p>
    <script type = "text/javascript">
    function calc() {
    var a = parseFloat(document.myform.nput1.value);
    if (isNaN(a)) {
    alert ("You must enter a numeric value!");
    document.myform.nput1.value = "";
    return false;
    var res = (111.33 - (0.42 * a)).toFixed(2);  // result to 2 decimal places
    document.myform.result.value = res;
    document.getElementById("rlt").innerHTML = "The result of the calcultion is " + res;

    You may wish to improve the validation to check that the value entered is within a sensible range, min and max, and not negative. You give no details so this will be up to you. The result turns negative if an initial value > 265 is entered.

    "This is the worst disaster in California since I was elected." - California Governor Pat Brown, discussing a local flood
    Last edited by Philip M; 07-26-2009 at 08:42 PM.

  • Users who have thanked Philip M for this post:

    Plato (07-26-2009)

  • #3
    New Coder
    Join Date
    Aug 2008
    Thanked 0 Times in 0 Posts
    Brilliant, thanks


    Posting Permissions

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