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!!!
[code=php]
<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:”);
p = parseInt(productNumber);
while (productNumber != -1)
{
switch (p)
{
case 1: price = 2.98;
quantity = window.prompt(“Enter quantity sold for product 1:”);
q = parseInt(quantity);
pCost = price * q;
break;
case 2: price = 4.50;
quantity = window.prompt(“Enter quantity sold for product 2:”);
q = parseInt(quantity);
pCost = price * q;
break;
case 3: price = 9.98;
quantity = window.prompt(“Enter quantity sold for product 3:”);
q = parseInt(quantity);
pCost = price * q;
break;
case 4: price = 4.49;
quantity = window.prompt(“Enter quantity sold for product 4:”);
q = parseInt(quantity);
pCost = price * q;
break;
case 5: price = 6.87;
quantity = window.prompt(“Enter quantity sold for product 5:”);
q = 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:”);
p = 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>