Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: Brick Wall Calculator Nan??

07022012, 01:46 AM #1
 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 httpequiv="Contenttype" content="text/html; charset=UTF8">
<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>
07022012, 02:08 AM
#2
l, h & d are undefined variables.onClick="calculate(l, h, d)
07022012, 02:27 AM
#3
 Join Date
 Dec 2011
 Posts
 22
 Thanks
 2
 Thanked 0 Times in 0 Posts
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);
07022012, 05:03 AM
#4
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 (07022012)
07022012, 10:56 AM
#5
 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
07022012, 11:10 AM
#6
 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