www.webdeveloper.com
Results 1 to 2 of 2

Thread: Having trouble with some code

  1. #1
    Join Date
    Aug 2013
    Posts
    1

    Exclamation Having trouble with some code

    Hello, I am trying to develop a bit of code for my assignment. this is my first time using JavaScript and I have most of my code correct but I am missing something. Here is my code so far:

    <HTML>
    <HEAD>
    <TITLE>Gas Usage</TITLE>
    <SCRIPT LANGUAGE = JavaScript>
    var customerId = prompt ("Enter the customers ID number", -1)
    var gasUsage = prompt ("Enter customers gas usage", 0)
    var totalCustomers = 0
    var totalAmountOwing = 0
    while(customerId != -1) {
    if (gasUsage <= 60) {
    amountOwing = gasUsage * 2
    }
    else if (gasUsage > 60) {
    amountOwing = (gasUsage - 60) * 1.5 + 105
    }
    document.write("Customer ID: " + customerId)
    document.write(amountOwing)
    document.write("<br/>")
    customerId = prompt("Enter a Customer ID number", -1)
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

    This is what I'm trying to display on the web page:

    Customer ID: 1 has gas usage of 80 and owes $135
    Customer ID: 2 has gas usage of 45 and owes $90
    Customer ID: 3 has gas usage of 90 and owes $150
    Customer ID: 4 has gas usage of 34 and owes $68
    Customer ID: 5 has gas usage of 60 and owes $120
    Customer ID: 6 has gas usage of 120 and owes $195
    Total amount of customers is 6
    Total amount for all customers is $758

    This is the outcome when I run the code that I have:
    1. It prompts to enter the customer id
    2. It prompts for the gas usage
    3. It prompts for the second customer id
    4. It continues to prompt for the customer id until I enter -1
    5. Depending on how many time I enter a value that isn't -1, it displays something like this:
    Customer ID: 1135
    Customer ID: 2135

    So it's not prompting for the gas usage after the first gas usage prompt and just keeps displaying the first calculation next to each customer id I enter.

    Could somebody please tell me what I'm doing wrong?

    Thanks,
    Matthew Quan

  2. #2
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,472

    forgot totalCustomers and totalAmountOwing

    Hi there MatthewQuan94,

    and a warm welcome to these forums.

    The attribute "LANGUAGE = JavaScript" is deprecated in favour of type="text/javascript" .
    and the use of "document.write()" should be avoided.
    Use the DOM "document.getElementById()" instead.

    Try this code modification...
    Code:
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    <meta charset="utf-8">
    
    <title>untitled document</title>
    
    <script type="text/javascript">
    
       var totalCustomers=0;
       var totalAmountOwing=0;
    
    function init() {
    
       var customerId;
       var gasUsage;
       var agree;
    
    
    while(isNaN(customerId)) {
       customerId=prompt('Enter the customers ID number','id number');
     }
    if(customerId==null){
       return;
     }
    
    while(isNaN(gasUsage)) {
       var gasUsage=prompt('Enter customers gas usage','gas usage');
     }
    if(gasUsage==null){
       agree=confirm('another customer?');
    if(agree==true) {
       return init();
      }
     }
    
    if(gasUsage<=60) {
       var amountOwing=gasUsage*2;
     }
    else {
    if(gasUsage>60) {
       amountOwing=(gasUsage-60)*1.5+105;
      }
     }
    
       totalCustomers++;
       totalAmountOwing=totalAmountOwing+amountOwing;
    
       document.getElementById('results').innerHTML+=
       'Customer ID: '+customerId+' has gas usage of '+gasUsage+' and owes $'+amountOwing+'<br>';
    
       agree=confirm('another customer?');
    if(agree==true) {
       return init();
      }
    else {
       document.getElementById('results').innerHTML+=
       'Total amount of customers is '+totalCustomers+'<br>Total amount for all customers is $'+totalAmountOwing;
      }
     }
       window.addEventListener?
       window.addEventListener('load',init,false):
       window.attachEvent('onload',init);
    </script>
    
    </head>
    <body>
    
    <div id="results"></div>
    
    </body>
    </html>
    coothead
    Last edited by coothead; 08-02-2013 at 05:06 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles