Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 7th, 2009, 07:45 PM
Registered User
 
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
 
Old September 8th, 2009, 01:22 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 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
Click the "Thanks" button if this post helped you.
 
Old September 8th, 2009, 05:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 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 05:16 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner seeks advice - Address Book WazMeister Visual Basic 2008 Essentials 3 June 18th, 2008 10:14 AM
need advice from you alsafwa C# 2005 12 September 16th, 2006 07:44 AM
some advice please? liquidmonkey Java Basics 1 May 4th, 2006 08: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.