Java Script prompts

Discussion in 'Web Design and Development' started by mikepro44, Jul 14, 2009.

  1. mikepro44 macrumors member

    Joined:
    May 18, 2008
    #1
    Using java script I'm trying to have so you are prompted to put numbers in until you press 0, after that it will calculate and add all the numbers you put in. I think I'm getting pretty close to it..?

    Anyways, if someone could help me out that would be awesome:


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <script type="text/javascript">
    var price;
    var total;
    var temp = 0
    tax = 1.07
    prompt("please enter price");
    while(!price)
    {
    price = parseFloat(prompt("please enter a price or 0 to stop"));
    if(price != 0)
    {
    temp+=price;
    }
    }

    document.write("You total is $"+total.toFixed(2));
    </script>
    </body>
    </html>
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Here you go,
    PHP:
    <script type="text/javascript">
    function 
    PromptUser() {
      var 
    total 0;
      
    tax 1.07;
      do {
        var 
    price parseFloat(prompt("please enter a price or 0 to stop"));
        if (
    price !== 0) {
          
    total += price;
        }
      } while (
    price !== 0);
      
    OutputTotal(total);
    }
    function 
    OutputTotal(t) {
      
    document.getElementById('total').innerHTML "You total is $"t.toFixed(2);
    }
    window.onload PromptUser;
    </script>
    </head>
    <body>
    <div id="total"></div>
    </body>
    </html>
     

Share This Page