|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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) {
|
|
#3
|
|||
|
|||
|
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(); |
|
#4
|
||||
|
||||
|
Yeah, noticed that too, funny thing was my browser didn't care.. I could index the fourth element without any arrayoutofindex error..
![]() |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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>
|
|
#8
|
|||
|
|||
|
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 */ } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Back to basics - Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|