View Full Version : Brick Wall Calculator Nan??

07-02-2012, 02:46 AM
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 :o

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.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<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;
<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">
<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>
<input name="reset" value="Reset" type="reset">

Logic Ali
07-02-2012, 03:08 AM
onClick="calculate(l, h, d)

l, h & d are undefined variables.

07-02-2012, 03:27 AM
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);

07-02-2012, 06:03 AM
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:

<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;

<input type="button" value="Calculate" onClick="calculate();">

07-02-2012, 11:56 AM
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 :)



07-02-2012, 12:10 PM
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". :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum