Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2012, 01:28 PM   PM User | #1
realzahed
New Coder

 
Join Date: Apr 2012
Posts: 11
Thanks: 10
Thanked 0 Times in 0 Posts
realzahed is an unknown quantity at this point
Post syntax error??

Can someone point where i'm going wrong? I want to evaluate the values of 'miles' to be >=0 and 'putime' to between 0 and 24 only.
Code:
<script type = "text/javascript">
	function TaxiFare() {
	// calculates taxi fare based upon miles travelled
	// and the hour of the day in military time (0-23).

	var baseFare = 2.50;
	var costPerMile = 2.00;
	var nightSurcharge = 0.50; // 8pm to 6am, every night

	var milesTravelled = Number(document.getElementById("miles").value);
	var pickupTime = Number(document.getElementById("putime").value);

	var cost = baseFare + (costPerMile * milesTravelled);

	// add the nightSurcharge to the cost if it is after
	// 10pm or before 6am
	if (pickupTime >= 22 || pickupTime < 6) {
	cost += nightSurcharge;
	}
	if((document.getElementById('miles').value>=0)&&(document.getElementById('putime').value>=0||<=24) {
	alert("Your taxi fare is $"  + cost.toFixed(2));
	}
	else{return 0;}
	}

	</script>
realzahed is offline   Reply With Quote
Old 04-08-2012, 03:28 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You already have a thread on this topic!

if((document.getElementById('miles').value>=0)&&(document.getElementById('putime').value>=0||<=24) {

Use your error console to identify syntax errors! And inform the user what the problem is.

Code:
var milesTravelled = Number(document.getElementById("miles").value) || 0;
if ((milesTravelled < 1) || (milesTravelled > 200)) {
alert ("You must enter 1 - 200 miles");
document.getElementById("miles").focus();
return false;
}

var pickupTime = Number(document.getElementById("putime").value) || 0;
if ((pickupTime < 0) || (pickupTime > 23)) {  // note the hours are 0-23.  There is no 24 hour, midnight is 0 hours
alert ("The time must be 0-23 hours");
document.getElementById("putime").focus();
return false;
}

var cost = baseFare + (costPerMile * milesTravelled);

If I were you I would check the assignment to confirm that the extra night charge is supposed to be a flat $0.50 and not $0.50 per mile which is what I would expect.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 04-08-2012 at 03:48 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
realzahed (04-08-2012)
Old 04-08-2012, 04:03 PM   PM User | #3
realzahed
New Coder

 
Join Date: Apr 2012
Posts: 11
Thanks: 10
Thanked 0 Times in 0 Posts
realzahed is an unknown quantity at this point
yes it's a flat $0.50 and not $0.50 per mile.
realzahed is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, syntax, taxifare

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:42 AM.


Advertisement
Log in to turn off these ads.