p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Web Programming > JavaScript > Javascript
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 7th, 2009, 08:45 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginner - advice about if else statements

I am an absolute beginner with all programming. I am currently completing a course through independent study, but am stuck and have not had a response from the instructor.

I am required to use Javascript code to calculate sales tax on 3 sales totals entered. I think I understand if else statements and loops, but I don't know how to do this referencing the constants he has given us?


SALES_LIMITS = new Array (
0, // For bracket 0
50, // 1
100, // 2
250, // 3
500, // 4
);


TAX_RATES = new Array (
0.00, // For bracket 0
0.05, // 1
0.075, // 2
0.10, // 3
0.20 // 4);

Can anyone explain how to do this to me. Please be nice I am very new at all of this.

Thanks

Em
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old September 8th, 2009, 02:22 AM
Friend of Wrox
Points: 1,882, Level: 17
Points: 1,882, Level: 17 Points: 1,882, Level: 17 Points: 1,882, Level: 17
Activity: 7%
Activity: 7% Activity: 7% Activity: 7%
 
Join Date: May 2004
Location: India
Posts: 563
Thanks: 0
Thanked 14 Times in 14 Posts
Default

Assuming you have following requirement, You need to calculate tax as follows:
IF SALES_LIMITS is between
0-50 0.75
50-100 0.075
100-25 0.10
> 500 0.20

Code:
function Check() 
{
var salesAmt = 67; 
var taxAmt = 0; 
if(salesAmt >=0 && salesAmt <=50) { 
taxAmt = salesAmt*0.05; } 
else if(salesAmt>50 && salesAmt <=100) { 
taxAmt = salesAmt*0.075; } 
else if(salesAmt>100 && salesAmt <=250) { 
taxAmt = salesAmt*0.10; } 
else if(salesAmt>250 && salesAmt <=500) { 
taxAmt = salesAmt*0.20; } 
else { 
taxAmt = salesAmt*0.20; 
} 
salesAmt = salesAmt + taxAmt 
alert(salesAmt); }
__________________
Om Prakash Pant
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old September 8th, 2009, 06:13 PM
Friend of Wrox
Points: 4,805, Level: 29
Points: 4,805, Level: 29 Points: 4,805, Level: 29 Points: 4,805, Level: 29
Activity: 38%
Activity: 38% Activity: 38% Activity: 38%
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
Default

Except this code is a bit overkill.

After all, if your IF does "... && salesAmt <= 50 ...", then there is no need to test for > 50! BY DEFINITION you wouldn't REACH the next IF unless salesAmt is > 50.

Also, if you want to make it MUCH more flexible, you SHOULD base the code on the arrays, as described in the original post. Except I'd use a "unified" array insted of two separate ones.

So:
Code:
var taxBrackets = [ [50, 0.00], [100, 0.05], [250, 0.075], [500, 0.10], [1000000000, 0.20] ];
 
function getTaxForSale( saleamt )
{
    for ( var t = 0; t < taxBrackets.length; ++t )
    {
        var bracket = taxBrackets[t];
        if ( saleamt < bracket[0] ) return bracket[1];
    }
    // should never get here, unless the sale is over 1000000000
    return 0.20; // or whatever the rate for over 1000000000 should be
}
A variation on that, so we don't have to worry about the 1000000000, even:
Code:
var taxBrackets = [ [50, 0.00], [100, 0.05], [250, 0.075], [500, 0.10], [null, 0.20] ];
 
function getTaxForSale( saleamt )
{
    for ( var t = 0; t < taxBrackets.length; ++t )
    {
        var bracket = taxBrackets[t];
        if ( bracket[0] == null || saleamt < bracket[0] ) return bracket[1];
    }
}
And, of course, if you preferred to return the tax *AMOUNT* instead of just the rate, then simply do
Code:
 
        if ( bracket[0] == null || saleamt < bracket[0] ) return saleamt * bracket[1];
instead.

Last edited by Old Pedant : September 8th, 2009 at 06:16 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner seeks advice - Address Book WazMeister Visual Basic 2008 Essentials 3 June 18th, 2008 11:14 AM
need advice from you alsafwa C# 2005 12 September 16th, 2006 08:44 AM
some advice please? liquidmonkey Java Basics 1 May 4th, 2006 09:54 AM
Need some advice wariental HTML Code Clinic 1 March 25th, 2006 09:48 PM
Advice 95_rifleman VB Databases Basics 2 February 13th, 2006 08:15 AM



All times are GMT -4. The time now is 04:08 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc