/    Sign up×
Community /Pin to ProfileBookmark

Quick arithmetic problem…

Hi,

Hoping someone might be able to help.

I am pretty new to Javascript, but am learning and will get there some day.

The first ‘if’ statement works where it gives a 10% discount if the conditions are met.

In the second ‘if’ statement, I am trying to put a discount of 100bucks off if you purchase more than 11 of the items and obviously the other conditions are met.

I want the new price to sit in ‘totalprice’

[CODE] if(bbclubmember==1 && priceArr[cnt][5]==1)
{
discount +=(itemprice * 0.01); // 10 percent discount for club members
if(itemprice>0)
{
UTMI += “UTM:I|” + dateid +”|”+namer+”|”+namer+”|”+namer+”|n”+filler((priceArr[cnt][3] * 0.9),2)+”|”+ numberofitem +”n”;
}
}

if(bbclubmember==1 && priceArr[cnt][5]==2 && totalnum >11)
{
discount +=(itemprice – 100); // 100 bucks discount for club members
if(itemprice>0)
{
UTMI += “UTM:I|” + dateid +”|”+namer+”|”+namer+”|”+namer+”|n”+filler(priceArr[cnt][3],2)+”|”+ numberofitem +”n”;
}
}

totalprice += itemprice;

[/CODE]

I thought I should just be able to put ‘discount +=(itemprice – 100);’ to take 100bucks of the total price but instead it puts the final price as ‘100’. The rest of the statement seems to work as it doesn’t start putting a discount in until you select ’12’ or over as the number of items.

What it does is:

Say 15 items are $900,

It will have: Total amount = $900
Total Discount = $800
Net Amount to Pay = $100

Where I want: Total Amount = $900
Total Discount = $100
Net Amount to Pay = $800

I hope this is enough for you to help me out, I have taken fully functional script to slightly edit so it all works well apart from this, of course there may well be something obvious in another part of the script that I a missing. I basically copied the first ‘if’ and tried to slightly adjust it to sort out another ‘discount’ scenario.

Any pointers would be much appreciated

Cheers

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MalgrimJul 20.2009 — Well, there's a number of things, that strike me as odd in your code.

For one thing discount +=(itemprice * 0.01) sets the discount to 1% not 10%.

discount +=(itemprice - 100) adds all but 100 to the discount which likely causes the problem you mentioned, but since you didn't post the code where you handle the discount, there's no way to say for sure.

Assuming itemprice refers to a single item, the codesnippet might be executed for each item thus adding the 100$ discount (actually the all but 100$ discount) as many times as there are items.

Moreover, shouldn't there be some kind of rule that the total cost of the 12+ items is actually over 100$?

Maybe that helps somehow.
Copy linkTweet thisAlerts:
@mr_dobalinoauthorJul 20.2009 — Malgrim,

Thanks for helping out, after you explaining it a little I figured out all I needed to do to complete what I was attempting was to put 'discount=(100)'

brilliant,

thanks for your help
×

Success!

Help @mr_dobalino 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...