Javascript Battery Circuit

Create a function called totalSales(). The function will use a loop to ask the user to enter amounts until they enter the word "done." The body of the loop will add the amount of input to a variable called totalSales. When the loop ends, the function should display the value of totalSales in the warning.

Here is what I have ...

function totalSales()
{
     var x = prompt("Enter numeric until done")
     var amount="";
     var totalSales=0;

     while(x!=="done") { 
         x = prompt("Enter numeric until done") 

         if(x==="done")
         {
             alert(totalSales += amount);       
         } 
     }  
 }

My onclick event handler works, but I'm not sure how to add an unknown number to the variable. I can't get it to add instead of concatenation.

+4
source share
1 answer

Use parseIntto convert a string value to an integer value.

function totalSales()
{
   var x = prompt("Enter numeric until done")
   var amount = "";
   var totalSales = 0;

   while(x!=="done")
   { 
        x = prompt("Enter numeric until done");

        if(x==="done")
        {
            alert(totalSales);
        } 
        else if(!isNaN(x)) 
        {
            totalSales += parseInt(x, 10);
        }
    }

}
+2
source

Source: https://habr.com/ru/post/1537607/


All Articles