/    Sign up×
Community /Pin to ProfileBookmark

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!!!

[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>

[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@grumpyOleManMar 14.2013 — 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);[/CODE]



to this

[CODE]totalSum += pCost;[/CODE]
Copy linkTweet thisAlerts:
@glopez810authorMar 14.2013 — thank you, i understand now!
×

Success!

Help @glopez810 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...