Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: syntax error??

  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    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.
    Last edited by Philip M; 04-08-2012 at 04:48 PM.

    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.

  • Users who have thanked Philip M for this post:

    realzahed (04-08-2012)

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    11
    Thanks
    10
    Thanked 0 Times in 0 Posts
    yes it's a flat $0.50 and not $0.50 per mile.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •