/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Problem adding 2 variables!!!!

Okay, i feel stupid asking this javascript question, but i’m new to this, so here goes:

function tax_price()
{
var tax = 0;

for ( i = 0; i < top.selectedProducts.length; i++)
{
tax = tax + (top.selectedProducts[i].cost) * (0.05);
}

tax = roundOff(tax);
document.order.Tax.value = tax;

total_price();
}

function total_price()
{
var subCost = 0;

for ( i = 0; i < top.selectedProducts.length; i++)
{
subCost = subCost + top.selectedProducts[i].cost;
}

tax = document.order.Tax.value;
total = subCost + tax;
document.order.Total.value = total;
}

The above 2 functions should display the total cost of any number of products in a shopping cart. And it does to an extend. For example: The cost (subCost) of 3 products is 225. The tax might be something like 17.25. Therefore the total should be 242.25. However it displays 22517.25.

I assume that it is treating the values as strings, and i don’t understand why. How do i sort out this product?

Here is little bit more info on the code.

selectedProducts is an array in my top page(where my frames are). This array stores all the info on selected products form the website, such as the price, qty, cost, name etc.
The above 2 functions are called after the user leaves an input field (onBlur event ).

I can provide more code if it is any help to you in sloving this problem.

Thanks in advance for any help.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@benzinoauthorApr 12.2006 — Problem solved.

Solution: total = Number(subCost) + Number(tax);
×

Success!

Help @benzino 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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