www.webdeveloper.com
Results 1 to 3 of 3

Thread: what is wrong with this?!

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    4

    what is wrong with this?!

    new to javascript, had an assignment. basically, user enters a product number, my switch statement determines the price, then the user enters a quantity and, and my script is supposed to total everything that is entered. it doesn't do it and i don't know why. not allowed to use arrays. help!!!


    PHP Code:

    <html>
    <
    head>
        <
    title>Untitled</title>
        <
    script type="text/javascript">
            
            var 
    productNumber 0;
            var 
    p//convert product number to int
            
    var quantity;
            var 
    q//convert quantity number to int
            
    var price;
            var 
    pCost 0//price * quantity
            
    var totalSum;
            var 
    counter 0;
            
            
    totalSum 0;
            
    //capture number
            
    productNumber window.prompt ("Enter product number for an item (1-5), Enter -1 to quit:");
            
            
    parseInt(productNumber);

            while (
    productNumber != -1)
                {
            
                switch (
    p)
                    {
                        case 
    1price 2.98
                            
    quantity window.prompt("Enter quantity sold for product 1:");
                            
    parseInt(quantity); 
                            
    pCost price q;
                        break;
                        
                        case 
    2price 4.50
                            
    quantity window.prompt("Enter quantity sold for product 2:");
                            
    parseInt(quantity); 
                            
    pCost price q;
                        break;
                        
                        case 
    3price 9.98
                            
    quantity window.prompt("Enter quantity sold for product 3:");
                            
    parseInt(quantity); 
                            
    pCost price q;
                        break;
                        
                        case 
    4price 4.49
                            
    quantity window.prompt("Enter quantity sold for product 4:");
                            
    parseInt(quantity); 
                            
    pCost price q;
                        break;
                        
                        case 
    5price 6.87
                            
    quantity window.prompt("Enter quantity sold for product 5:");
                            
    parseInt(quantity); 
                            
    pCost price q;
                        break;
                        
                        default: 
    window.alert("Please try again");
                        break;
                    
                    }
            
    counter += 1;
            
    totalSum+= parseInt(pCost);
            
    productNumber window.prompt ("Enter product number for an item (1-5), Enter -1 to quit:");
            
    parseInt(productNumber);
                }
                
        if (
    counter !=0)
            {
                
    document.writeln("<h1> Total sales for the week : $" totalSum "</h1>");
            }
            else
                {
                
                    
    window.alert("No product number is entered");
                
                }
            

        
    </script>
    </head>

    <body>


    </body>
    </html> 

  2. #2
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    heaps wrong... heaps

    but lets not get to carried away..

    You seem to like the parseInt function a bit to much...

    first thing to do is use it properly, it needs a radix.

    next it returns an INTEGER..... NOT a number with a decimal included

    now change this

    Code:
    totalSum+= parseInt(pCost);


    to this

    Code:
    totalSum +=  pCost;

  3. #3
    Join Date
    Mar 2013
    Posts
    4
    thank you, i understand now!

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