# Thread: Function for Computing Square Feet and Yard

1. Here's a clue. The answer is in post #4 in the middle paragraph.

2. Registered User
Join Date
Nov 2012
Posts
16
Originally Posted by wbport
Here's a clue. The answer is in post #4 in the middle paragraph.
well...that's easier than i expected lol

3. Registered User
Join Date
Oct 2010
Location
Versailles, France
Posts
1,290
Read this w3school.com «Basic examples» page and particularly the first links and Examples explained

Code:
```<html><head><title>Carpet Calculator</title>
<script language="JavaScript">
function verifyOverage() {
var iOverage, fOverage;
if ((iOverage = parseInt(document.carpet.overage.value)) != (fOverage = parseFloat(document.carpet.overage.value)))
return -1;
else
if (iOverage >= 0 && iOverage <= 20)
return iOverage;
else
return -1;
}
function calculate(parm) {
var overage = verifyOverage()
if (overage == -1)
alert("Overage must be an integer percent in the range 0-20");
else {
answer = (document.carpet.length.value * document.carpet.width.value * (1 + overage / 100))/parm;
var wHeight = 80
var wWidth  = 475
var AvailWidth  = window.screen.availWidth
var AvailHeight  = window.screen.availHeight
var hOffset = parseInt(AvailWidth/2) - parseInt(wWidth/2)
var vOffset = parseInt(AvailHeight/2) - parseInt(wHeight/2)
var wOption = "resizable,status,scrollbars,width=" + wWidth + ",height=" + wHeight +
",left=" + hOffset+ ",top=" +vOffset

var w = window.open("","gentable",wOption);
html += (parm == 9)? "yards":"feet";
html += ".</body></html>";
w.document.write(html);
w.document.close(); }

}
function ComputeSquareFeet() {
calculate(1); }
function ComputeSquareYards() {
calculate(9); }

</script>
<body>
<h1>Carpet Calculator</h1>
<form name = "carpet" action=" ">
</br>Enter the length of your room in feet</br><input name = "length" type = "text" />
</br>Enter the width of your room in feet</br><input name = "width" type = "text" />
</br>Typically an allowance is made for room irregularities and unavoidable waste.
</br>Enter the percent overage as an integer in the interval [0, 20]</br><input name = "overage" type = "text" />
</br></br><input name = "SqFt" type = "button" value = "Compute Square Feet   " onclick = "ComputeSquareFeet()" />
</br></br><input name = "SqYd" type = "button" value = "Compute Square Yards" onclick = "ComputeSquareYards()" />
</br></br><input type = "reset" value = "Clear" />
</form>
</body>
</html>```

5. Registered User
Join Date
Nov 2012
Posts
16
yea my code looked like a 1st grade version of yours
thanks a ton for your help. i'm sure i'll be making a new thread since my next assignment is due on wednesday lol

6. You're welcome, but make sure you are a lot further along before coming to this forum. This should be the last resort, not the first. Someone on a programming forum I used to visit was notorious for a curt "Do your own homework," message when a student posted a question without showing any indication any work had been put in on it.

7. Registered User
Join Date
Nov 2012
Posts
16
yea i always try my best to learn from the books, but it's never enough. people on forums always seem to help me get my mind going in the right direction for learning how to do things myself. plus i was kinda just thrown into javascript without any kind of lesson so having someone with experience guide me along the right way is a huge help. my mind was stuck on VB so my functions weren't working until i was able to compare both of our scripts and see what i was missing (which was quite a bit lol). thanks again buddy

8. Registered User
Join Date
Dec 2012
Posts
1
Hello,
I am working on the same and stuck with the exactly same problem with document.write. Can you please help me with the same?
(I also have same HTML code you have posted here)
Thanks a lot
-Ashjay