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 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Brick Wall Calculator Nan??

    Hi there,

    Right hopefully I have managed to post this time in the correct forum page!! Sorry to the Admins for my previous posting errors

    I am working on a algerba to JS formula conversion for a calculator and need some help as I am currently getting NaN with the code below.

    Pretty much it is to work out area of a brick wall minus a round window and the associated costs. I know my algebra is on the money, just need some assistance in the coding conversion.

    Thanks for taking a look.

    BP

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <title>Brick Wall Cost Calc</title>
    <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
    <meta name="author" content="">
    <script type="text/javascript">
    var x = 30;
    var y = 20;
    function calculate(l, h, d){
    var window = Math.PI * Math.pow(d, 2) / 4;
    var costTotal = x * ((l * h) - window) + y * window;
    if (document.getElementById("Sum").value = Cost);
    return Cost;
    };
    </script>
    </head>
    <body>
    <h1>Cost of Brickwall with a Round Glass Window</h1>
    <form id="aggregateForm" name="aggregateForm" action="">
    <input type="text" id="l" size="2" value="length">
    <input type="text" id="h" size="2" value="height">
    <input type="text" id="d" size="5" value="diameter">
    </br>
    </br>
    <input type="button" value="Calculate" onClick="calculate(l, h, d);">
    <h4>Cost of wall</h4>
    <input type="text" id="Cost" size="10" value="$0.00"></br>
    </br>
    <input name="reset" value="Reset" type="reset">
    </form>
    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    onClick="calculate(l, h, d)
    l, h & d are undefined variables.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by Logic Ali View Post
    l, h & d are undefined variables.
    Hi Ali,

    Thanks for taking a look.

    How do you mean I haven't declared them? They are part of the parameters of the function and are user dependant values.

    Below is what I came up with on paper, I just don't know how to integrate it into my code properly.

    // x is the cost per sqm for brick
    var x = 30;
    //y is the cost per sqm for glass
    var y = 20;

    // ’h’ = height of wall, ‘l’ = length of wall and ‘d’ diameter of the round window
    var costBrickWall = function (l, h, d) {

    //Math.PI will return a value of 3.141592653589793, for a more exact value use 3.14
    //window = Area of Circle this function is using pi diameter square divided by 4
    var window = Math.PI * Math.pow(d,2)/4;
    return x * ((l * h) - window) + y * window;
    };
    //call the function
    costBrickWall(l, h, d);

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    you don't need to pass those values to the function in the onclick but you do need to reference them correctly (and as long as x and y are only used by one function you may as well keep them local. one other thing - "window" is a javascript keyword. you can get away with using it, but it's better off avoided:

    Code:
    <script type="text/javascript">
    
    function calculate(){
    var x = 30;
    var y = 20;
    var ln=document.getElementById("l").value;
    var ht=document.getElementById("h").value;
    var di=document.getElementById("d").value;
    var thewin = Math.PI * Math.pow(di, 2) / 4;
    var costTotal = x * ((ln * ht) - thewin) + y * thewin;
    document.getElementById("Cost").value = costTotal;
    };
    </script>
    
    <input type="button" value="Calculate" onClick="calculate();">

  • Users who have thanked xelawho for this post:

    Blowpipe (07-02-2012)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Xelawho,

    Thanks for the heads up about the "window" keyword, I wasn't aware of that one.

    And thanks for taking a look at what I had and helping me to improve my coding

    Cheers

    BP

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    2
    Thanked 0 Times in 0 Posts
    A big thanks to everyone who had a look at what I had and helped me with it.

    It is good to know that I wasn't that far off actually having the correct code, and that I need to remember "declare all variables".

    Cheers

    BP


  •  

    Posting Permissions

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