JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 14th, 2006, 02:54 AM
darcey123 darcey123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 darcey123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 16 sec
Reputation Power: 0
Red face Back to basics - Arrays

Hey sorry to be a pain but im sort of new to javascript and i have a piece of script that doesnt seem to work it is for an assignment that i have to complete. Heres the code im using:

Code:
function Results(fluid, percent, hour, weight) {
	var okay_to_drive = (fluid * percent * 0.075 / weight) - (hour * 0.015);
	var okay_to_drive = okay_to_drive.toFixed (3);
	
	Comment = new Array(3)
	Comment[0] = "Okay to Drive"
	Comment[1] = "Under legal limit, but possible impairment of judgement"
	Comment[2] = "Don't Drive!! You are way over the legal limit"
	Comment[3] = "Yeah, might be a good idea to stop drinking now"
	
	alcohollevel.value = okay_to_drive;

	if (txtalcohollevel.value <= 0) {
		txtsuggestion.value= Comment[0];
	}
	else if (txtalcohollevel.value <= 0.05) {
		txtsuggestion.value= Comment[1];
	}
	else if (txtalcohollevel.value <= 0.2) {
		txtsuggestion.value= Comment[2];
	}
	else (txtalcohollevel.value > 0.2) {
		txtsuggestion.value= Comment[3];
	}		
}


Now all you advanced coders will be like "duh!" i can see it now its probably really obvious but if you could let me know what you think is wrong (or even easier/cleaner ways of doing the same thing)

Thanks in advanced
Darcey

Reply With Quote
  #2  
Old May 14th, 2006, 04:51 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 734 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 57 m 2 sec
Reputation Power: 4
Well, I don't know what the error is you are getting but you may be confusing the variable declared here:
Code:
alcohollevel.value = okay_to_drive;

with the variable you want to use later on:
Code:
if (txtalcohollevel.value <= 0) {

Reply With Quote
  #3  
Old May 14th, 2006, 08:50 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 135 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 45 m 13 sec
Reputation Power: 4
Also, you have 4 indeces for Comments.
Code:
Comment = new Array(3)

should be
Code:
var Comment = new Array(4);

OR just this:
Code:
var Comment = new Array();

Reply With Quote
  #4  
Old May 15th, 2006, 01:41 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 734 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 11 h 57 m 2 sec
Reputation Power: 4
Yeah, noticed that too, funny thing was my browser didn't care.. I could index the fourth element without any arrayoutofindex error..

Reply With Quote
  #5  
Old May 16th, 2006, 09:49 AM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 3
Send a message via Yahoo to ravs
Smile

hey darcey123

what i think it is the error of parsing. i faced this kind of error many times in past.

if you are comparing values. javascript is not very smart in this so you have to parse it
use

parseInt() for int
parseFloat() for float
values

i think it will help you.

Reply With Quote
  #6  
Old May 16th, 2006, 03:33 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 135 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 45 m 13 sec
Reputation Power: 4
ravs, you're right. darcey123 does need to use parseFloat().

darcey123, alcohollevel, txtalcohollevel, and txtsuggestion aren't IDs or NAMEs of elements are they?

If they are please show us the X/HTML code for your form.

Reply With Quote
  #7  
Old May 17th, 2006, 06:33 AM
darcey123 darcey123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 darcey123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 45 m 16 sec
Reputation Power: 0
Here is all of my code that i am using sorry if some of it doesnt make sense it most of it is halfway through being changed. If anyone can help thanks in advanced

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"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>Blood Alcohol Check for Drivers</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">

function Validate_Data(fluid, percent, hour, weight)	{
	if ((fluid <= 0) || (percent <= 0) || (hour <=0) || (weight <=0)) {
		alert("In one or more of the fields there is a value missing, please enter a number greater than zero");
	}
	else {
		if (weight < 800) {
			Results(txtfluid.value, txtpercentage.value, txthour.value, txtweight.value);
		}
		else {
			alert("Please Enter a Weight Value Less Than 800 Pounds");
		}
	}
}

function Results(fluid, percent, hour, weight) {
	var okay_to_drive = (fluid * percent * 0.075 / weight) - (hour * 0.015);
	var okay_to_drive = okay_to_drive.toFixed (3);

	
	var Comment = new Array();
	Comment[0] = "Okay to Drive"
	Comment[1] = "Under legal limit, but possible impairment of judgement"
	Comment[2] = "Don't Drive!! You are way over the legal limit"
	Comment[3] = "Yeah, might be a good idea to stop drinking now"
	


	if (okay_to_drive <= 0) {
		document.getElementById('suggestion').value= Comment[0];
	}
	else if (okay_to_drive <= 0.05) {
		document.getElementById('suggestion').value= Comment[1];
	}
	else if (okay_to_drive <= 0.2) {
		document.getElementById('suggestion').value= Comment[2];
	}
	else (okay_to_drive > 0.2) {
		document.getElementById('suggestion').value= Comment[3];
	}		
}
</script>
<h2>Blood Alcohol Check for Drivers</h2>

<table border="0">
	<tr>
		<th>Fluid Ounces Consumed (Standard Drinks)</th>
		<th>Percentage of Alcohol</th>
	</tr>
	<tr>
		<td>Beer: 12oz</td>
		<td>Beer: 5%</td>
	</tr>
	<tr>
		<td>Wine: 4oz</td>
		<td>Wine: 12%</td>
	</tr>
	<tr>
		<td>Spirit: 1.5oz</td>
		<td>Spirit: 40%</td>
	</tr>
	
</table>
<br />
<br />
<br />
<table border="0">
	<tr>
		<td>Fluid Ounces Consumed:</td>
		<td><input type="text" name="txtfluid" id="fluid"/> (refer to table above)</td>
	</tr>
	<tr>
		<td>Percentage Alcohol</td>
		<td><input type="text" name="txtpercentage" id="percentage"/> (refer to table above)</td>
	</tr>
	<tr>
		<td>Drinks Consumed Over ___ Hours:</td>
		<td><input type="text" name="txthour" id="hour" /> Hours</td>
	</tr>
	<tr>
		<td>Your Weight In Pounds:</td>
		<td><input type="text" name="txtweight" id="weight"/> Pounds</td>
	</tr>
	<tr>
		<td><input type="button" name="calcresult" Value="Are You Okay To Drive?" onClick="javascript:Validate_Data(fluid.value, percentage.value, hour.value, weight.value);"></td>
	</tr>
</table>
<form name="frmresults">
		<h4>Results</h4>
		Your blood alcohol level is: 
		<input type="text" name="txtbal" id="bal" />
		<br/>
		Comment: 
		<input type="text" name="txtsuggestion" id="suggestion" size="50"/>
</form>

<h5>Disclaimer: The values and calculations provided here are for the purpose of a class assessment only. They should not be used as a basis for real-life decisions related to blood alcohol levels. No allowance has been made for variation in results between males and females. Alcohol content and measurements for Australian standard drinks may vary from amounts shown here.</h5>

</body>
</html>

Reply With Quote
  #8  
Old May 17th, 2006, 07:29 AM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 3
Send a message via Yahoo to ravs
hey darcey123

did you saw what i have send to you

you have to parse those values in to float then compare

like this

if(parseFloat(anyElement.value)==0.5)
{
/*Do What Ever you Want */
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Back to basics - Arrays


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT