home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Beginner, javascript code help!

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2020
Discussions: 200,974, Posts: 2,379,799, Members: 246,339
Old April 3rd, 2008, 04:46 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 3
Exclamation
Beginner, javascript code help!

So I'm a beginner with a crap teacher and an even worse book and now a take home test that's giving me the desire to punt things out my front door.

Can anyone give me direction on this? It's a little page with xhtml and javascript figuring area and cost of carpeting and a mouseover map (which I've got figured out).

function currency(amount) {

amount = Math.round(amount * 100); // convert amount value to pennies
amount = "" + amount; // convert amount to string
amount = amount.substring(0, amount.length - 2) + "." + amount.substring(amount.length - 2, amount.length);

return amount;

} // end currency function

var ctype = new Array("Berber", "Shag", "Astroturf", "Plush", "Commercial", "Loop Pile");
// var pic = new Array("assessment/north_america_1.gif","assessment/alaska.gif", "assessment/continental_us.gif", "assessment/greenland.gif", "assessment/mexico.gif");;

function calculate(long * wide) {

var firstname = document.areaform.fname.value;
var lastname = document.areaform.lname.value;
var long = document.areaform.long.value;
var wide = document.areaform.wide.value;

//
// Name checks
//
if (firstname == "") {
alert("Please enter your first name");
document.areaform.firstname.focus();
return false;
}

if (lastname == "") {
alert("Please enter your last name");
document.areaform.lastname.focus();
return false;
}

if (long.length <= 0){
alert("Please enter any amount above 1");
document.areaform.long.focus();
return false;
}

if (wide.length <= 0) {
alert("Please enter any amount above 1");
document.areaform.wide.focus();
return false;
}
else
return true;
calculate ?

//
// determine square footage of area to be carpeted
//


//
// for each type of carpet
//
for (ctr = 0; ctr < ???; ctr++) {

// check to see if this carpet type was selected and process accordingly

?

} // end for loop
alert (document.areaform.firstname + document.areaform.lastname + '&apos;s cost for ' + calculate + ' square feet of ' + document.areaform.ctype + ' carpet is ' currency''.')">
} // end calculate()
//-->
</script>
snikker74 is offline   Reply With Quote
Old April 3rd, 2008, 06:01 PM     #2 (permalink)
Junior Member
 
Join Date: Dec 2007
Posts: 20
So what's your problem? I'll help ya, but you need to be more specific as to what you are having issues coding.

I'll give you something that jumped right out at me:

var long = document.areaform.long.value;

long is a type, so don't use it as a variable name.

Also, use CODE brackets. ya know, so code can look like this:

Code:
function calculate(long * wide) {

	var firstname = document.areaform.fname.value;
	var lastname = document.areaform.lname.value;
	var long = document.areaform.long.value;
	var wide = document.areaform.wide.value;

}
Also, post the code of the XHTML page. It's always helpful to have that info.


-ng217
new_guy217 is offline   Reply With Quote
Old April 3rd, 2008, 06:24 PM     #3 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 3
the whole thing

There is brackets in there so I'm not sure what you're referring too. Mainly, the alert boxes and calculations aren't working (and I've followed his examples) I really don't know enough javascript to do this assignment, but we've moved on to PHP and I'm understanding that much better. I guess it's the object-oriented stuff that doesn't filter through.

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www/w3/org/TR/xhtml/11/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Web Programming II &minus; Assessment #2 &minus; my name</title>

<style type="text/css">

body {font-size:0.8em; font-style: Verdana; color: navy; background-color:#6699cc;}

</style>


<script type="javascript">
<!--

function currency(amount) {

amount = Math.round(amount * 100); // convert amount value to pennies
amount = "" + amount; // convert amount to string
amount = amount.substring(0, amount.length - 2) + "." + amount.substring(amount.length - 2, amount.length);

return amount;

} // end currency function

var ctype = new Array("Berber", "Shag", "Astroturf", "Plush", "Commercial", "Loop Pile");
// var pic = new Array("assessment/north_america_1.gif","assessment/alaska.gif", "assessment/continental_us.gif", "assessment/greenland.gif", "assessment/mexico.gif");;

function calculate(long * wide) {

var firstname = document.areaform.fname.value;
var lastname = document.areaform.lname.value;
var long = document.areaform.long.value;
var wide = document.areaform.wide.value;

//
// Name checks
//
if (firstname == "") {
alert("Please enter your first name");
document.areaform.firstname.focus();
return false;
}

if (lastname == "") {
alert("Please enter your last name");
document.areaform.lastname.focus();
return false;
}

if (long.length <= 0){
alert("Please enter any amount above 1");
document.areaform.long.focus();
return false;
}

if (wide.length <= 0) {
alert("Please enter any amount above 1");
document.areaform.wide.focus();
return false;
}
else
return true;
calculate

//
// determine square footage of area to be carpeted
//


//
// for each type of carpet
//
for (ctr = 0; ctr < ???; ctr++) {

// check to see if this carpet type was selected and process accordingly



} // end for loop
alert (document.areaform.firstname + document.areaform.lastname + '&apos;s cost for ' + calculate + ' square feet of ' + document.areaform.ctype + ' carpet is ' currency''.')">
} // end calculate()
//-->
</script>

</head>

<body>

<h2>Welcome to Bill's Carpet Warehouse</h2>
<form name="areaform" action="" method="get" onsubmit="alert (calculate)">

First Name: <input type="text" name="fname" /><br/><br/>

Last Name: <input type="text" name="lname" /><br/><br/>

Enter length of room: (in feet) <input type="text" name="long" /><br/><br/>

Enter width of room: &nbsp;(in feet) <input type="text" name="wide" /><br/><br/>


<table cellspacing="8">
<tr>
<td>
<map name="north_america_map" id="north_america_map" onmouseout="north_america_1.gif">
<area shape="poly" coords="15,48,31,57,49,62,63,45,59,34,42,36,35,48" onmouseover="alaska.gif" target="_self" alt="Alaska" />
<area shape="poly" coords="133,152,129,142,132,131,137,126,136,118,14 1,110,141,104,145,100,141,97,137,103,131,107,128,1 07,119,105,114,100,106,103,83,100,60,96,56,105,53, 110,54,122,59,133,64,136,73,140,80,142,86,146,92,1 46,96,153,105,144,112,143,124,142,130,149" onmouseover="continental_us.gif" href="continental_us.gif" target="_self" alt="Continental 48" />
<area shape="poly" coords="61,137,65,150,71,159,80,160,81,169,95,175, 104,176,111,180,114,173,121,165,114,164,109,171,10 1,171,98,163,97,154,90,147,84,148,79,142,70,142" onmouseover="mexico.gif" href="mexico.gif" target="_self" alt="Mexico" />
<area shape="poly" coords="116,13,111,19,111,30,114,36,128,40,132,43, 135,50,146,54,153,55,145,42,144,34,145,26,135,20,1 19,11" onmouseover="greenland.gif" href="greenland.gif" target="_self" alt="Greenland" />

</map>
<p><img src="north_america_1.gif" usemap="#north_america_map" name="north_america" border="0" alt="North America" height="241" width="184" />
</p>
</td>

<td>

Carpet choices:<br/>
<select name="choices" size="5">

<option value="5.99" selected="selected">Berber</option>
<option value="3.25">Shag</option>
<option value="9.25">Astroturf</option>
<option value="1.50">Plush</option>
<option value="2.00">Commercial</option>
<option value="2.50">Loop Pile</option>

</select>
<br/><br/>

</td>
</tr>
</table>


<input type="submit" name="submit" value="Calculate Cost" onclick="display_alert(calculate * amount)" />

</form>



</body>
</html>
snikker74 is offline   Reply With Quote
Old April 4th, 2008, 01:44 AM     #4 (permalink)
Junior Member
 
Join Date: Dec 2007
Posts: 20
[c o d e] [/ c o d e] brackets, in your post man (take out the spaces)

Here's a sample that does what you want:

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  	<title>Web Programming II - Assessment #2 - my name</title>
  	
  	<script type="text/css">
			body {
				font-size: 12px;
				font-style: Verdana; 
				color: navy; 
				background-color:#6699cc;
			}
 		</script>
 		
 		<script type="text/javascript">
 		//** Make usefull comments, if you must.
		//** Not like "end currency function"
			
		//** Displays cost of carpet
			function showCost(){				
			//**Create object instances
				var tbFirstName = document.getElementById('tbFirstName');
				var tbLastName = document.getElementById('tbLastName');
				var tbLength = document.getElementById('tbLength');
				var tbWidth = document.getElementById('tbWidth');
				var slCarpet = document.getElementById('slCarpet');
				
			//**Other variables used
			  var strErr = "";
			  var strFirstName = tbFirstName.value;
			  var strLastName = tbLastName.value;
			  var strLength = tbLength.value;
			  var strWidth = tbWidth.value;			  
			  
			//**Construct error message
				//**It's a good idea to clear our invalid input
				if(strFirstName.length < 1){						
					strErr = "Please enter your first name\n"; 
				}
				if(strLastName.length < 1){
				  strErr += "Please enter your last name\n"; 
			 	}
				if(isNaN(strLength) == true || strLength.length < 1){ 
					strErr += "Please enter a valid length\n"; 
					tbLength.value = "";
				}
				if(isNaN(strWidth) == true || strWidth.length < 1){ 
					strErr += "Please enter a valid width\n"; 
					tbWidth.value = "";
				}		
				
			//**Handle errors
				if(strErr != ""){
					alert(strErr);
				} 
				else {
			//**Calculate cost of carpet	
					var numArea = tbLength.value * tbWidth.value;
					var numSqFt = slCarpet.options[slCarpet.selectedIndex].value;
					
					var numCost = numArea * numSqFt;
					
					//Convert into USD string
					var strCost = curUSD(numCost);
					var strSqFt = curUSD(numSqFt);
					
			//**Construct and show messsge
					var strMsg = "Customer: " + tbFirstName.value + " " + tbLastName.value + "\n";
					strMsg += "Carpet Style: " + slCarpet.options[slCarpet.selectedIndex].text + "\n\n";
					strMsg += "Square Footage: " + numArea + "\n";
					strMsg += "Cost per sqFt: " + strSqFt + "\n\n";			
					strMsg += "Total Cost: " + strCost;
				
					alert(strMsg);
				}
			} 		
		
		//**Convert number to USD string
			function curUSD(num){
				var strNum = "" + num;
				
				//Handle errors
				if(isNaN(num)){ return "curUSD Function :: Not a Number"; }
				
				//Add second 0 or round to 2 digits
				if(strNum.charAt(strNum.length - 2) == "."){
					strNum = num + "0";
				}
				else {
					strNum = Math.round(num * 100) / 100;
				}
							
				return "$" + strNum;
			}
		</script>
  </head>
  
  <body>
  	<h2>Welcome to Bill's Carpet Warehouse</h2>

		First Name: <input type="text" id="tbFirstName" maxlength="50" /><br/><br/>		
		Last Name: <input type="text" id="tbLastName" maxlength="50" /><br/><br/>		
		Enter length of room: <input type="text" id="tbLength" maxlength="6" /><br/><br/>		
		Enter width of room: <input type="text" id="tbWidth" maxlength="6" /><br/><br/>		
		
		<table cellspacing="8">
		
			<tr>
				<td>
					<map name="north_america_map" id="north_america_map" onmouseout="north_america_1.gif">
						<area shape="poly" coords="15,48,31,57,49,62,63,45,59,34,42,36,35,48" onmouseover="alaska.gif" target="_self" alt="Alaska" /> 
						<area shape="poly" coords="133,152,129,142,132,131,137,126,136,118,14 1,110,141,104,145,100,141,97,137,103,131,107,128,1 07,119,105,114,100,106,103,83,100,60,96,56,105,53, 110,54,122,59,133,64,136,73,140,80,142,86,146,92,1 46,96,153,105,144,112,143,124,142,130,149" onmouseover="continental_us.gif" href="continental_us.gif" target="_self" alt="Continental 48" />
						<area shape="poly" coords="61,137,65,150,71,159,80,160,81,169,95,175, 104,176,111,180,114,173,121,165,114,164,109,171,10 1,171,98,163,97,154,90,147,84,148,79,142,70,142" onmouseover="mexico.gif" href="mexico.gif" target="_self" alt="Mexico" />
						<area shape="poly" coords="116,13,111,19,111,30,114,36,128,40,132,43, 135,50,146,54,153,55,145,42,144,34,145,26,135,20,1 19,11" onmouseover="greenland.gif" href="greenland.gif" target="_self" alt="Greenland" />
					</map>
					
					<p>
						<img src="north_america_1.gif" usemap="#north_america_map" name="north_america" border="0" alt="North America" height="241" width="184" />
					</p>
				</td>
		
				<td>				
					Carpet choices:<br/>					
					<select id="slCarpet" size="5">
						<option value="5.99" selected="selected">Berber</option>
						<option value="3.25">Shag</option>
						<option value="9.25">Astroturf</option>
						<option value="1.50">Plush</option>
						<option value="2.00">Commercial</option>
						<option value="2.50">Loop Pile</option>
					</select>	
				</td>
			</tr>
		</table>
	
		<input type="button" id="btShowCost" value="Calculate Cost" onclick="showCost();" />		

  </body>
</html>

-ng217
new_guy217 is offline   Reply With Quote
Old April 4th, 2008, 12:08 PM     #5 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 3
Thank you

Thank you, Thank you, Thank you, Thank you! You saved my butt!
snikker74 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner needs help mewelch Motherboards 1 February 1st, 2007 10:29 PM
Which linux for beginner?? Ultima Linux and Unix 17 February 16th, 2005 04:00 PM
im a beginner..... couch potato Webmastering and Programming 18 January 26th, 2003 09:44 PM
Quake3 beginner.... Mr. NoOne General Gaming Discussion 7 September 24th, 2002 02:14 AM
beginner with c++ and need help with an assignment Martster Webmastering and Programming 3 September 18th, 2002 09:02 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Health Care Rationing (5)
Is It Just Me? (3061)
Charges against non-tippers dropped.. (8)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (17)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
[F@H SPAM 11/16/09] ! 1/2 months to r.. (38)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
windows vista security holes (17)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)
virus blocking exe. files (1)
CPU fan stops spinning randomly (11)
Modern Warfare 2: Who Bought It? (65)


All times are GMT -4. The time now is 12:01 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28