0

I am receiving a NaN error when I run this code. I've been trying to find out why for a while now and can't for the life of me.

To clarify, I am trying to receive;

  • the total amount owed by customers

  • how many customers are entered

    function Summarise()
    {
    
        ID = prompt("Enter a Customer ID number ", -1)
    
        while(ID != -1) 
        {
            var gasUsed = 0
            var total = 0
            gasUsed = prompt("Enter amount of gas used ", "0")
            ///Standard Rate is used to assist in the calculation of customers in excess of 60 Gas Used
            StandardRate = 60 * 1.75
    
            if(gasUsed <= 60) 
            {
                total= gasUsed * 2
                document.write("Customers ID: " + ID)
                document.write(" has gas usage of " + gasUsed)
                document.write(" and owes $" + total)
                document.write("<br/>")
            } 
            else 
            {
                total= (gasUsed - 60) * 1.50 + StandardRate
                document.write("Customers ID: " + ID)
                document.write(" has gas usage of " + gasUsed)
                document.write(" and owes $" + total)
                document.write("<br/>")
    
            }
    
            var totalowed = total + totalowed
            var totalcustomers = ++totalcustomers
    
            ID = prompt("Enter a Customer ID number ", -1)
    
        }
    
        document.write("Total amount owed $" + totalowed)
        document.write("<br/>")
        document.write("Total Number of customers:" + totalcustomers)
    
    
    }
    
kix
  • 3,290
  • 27
  • 39

3 Answers3

0

totalowed and totalcustomers are declared in the while loop and thus not available outside it. You will need to define them outside the while loop.

FlorisLang
  • 21
  • 4
0

You need to define these two variables before the loop:

var totalowed = 0;
var totalcustomers = 0;

they were initially undefined when you were assigning values to them, this always create problems.

You can see the working snippet below:

function Summarise() {

ID = prompt("Enter a Customer ID number ", -1)
var totalowed = 0;
var totalcustomers = 0;
while(ID != -1) 
{
    var gasUsed = 0;
    var total = 0;
    gasUsed = prompt("Enter amount of gas used ", "0");
    ///Standard Rate is used to assist in the calculation of customers in excess of 60 Gas Used
    StandardRate = 60 * 1.75

    if(gasUsed <= 60) 
    {
        total = gasUsed * 2;
        document.write("Customers ID: " + ID);
        document.write(" has gas usage of " + gasUsed);
        document.write(" and owes $" + total);
        document.write("<br/>");
    } 
    else 
    {
        total= (gasUsed - 60) * 1.50 + StandardRate;
        document.write("Customers ID: " + ID);
        document.write(" has gas usage of " + gasUsed);
        document.write(" and owes $" + total);
        document.write("<br/>");

    }

    totalowed = total + totalowed;
    totalcustomers = ++totalcustomers;
    ID = prompt("Enter a Customer ID number ", -1);

}

document.write("Total amount owed $" + totalowed);
document.write("<br/>")
document.write("Total Number of customers:" + totalcustomers);
}

Summarise();
Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
-1

You are not converting the string from the prompt into an integer. See here:

You can use:

ID = parseInt(prompt("Enter a Customer ID number ", -1), 10);

Remember to provide a radix value to the parseInt function for completeness.

How to get numeric value from a prompt box?

Community
  • 1
  • 1
RichieAHB
  • 2,030
  • 2
  • 20
  • 33