1

The basis of the assignment is to use the if/else if statements to set up the script. I need a little help finishing up the if/else part and for someone to look over any errors. Here's the assignment:

Write the JavaScript code in one HTML document using IF, and IF/Else statements for the following three situations. For each one make sure to write comments for each section.

Determine tax rate based on income and what the tax would be on the income.

Variable declarations section 1. Declare a variable that holds the income amount entered by the user. 2. Declare a variable that holds the minimum income that will not be charged taxes. 3. Declare a variable that holds the tax percentage for tax bracket 1. 4. Declare a variable that holds the tax percentage for tax bracket 2. 5. Declare a variable that holds the highest income for tax bracket 1. 6. Declare a variable that holds the highest income for tax bracket 2.

Assignments section 7. Assign $1500 as the highest income amount that will not be charged taxes. 8. Assign the highest income for tax bracket 1 to be $25K and the tax percent to 15%. Anything over $25K is in the next tax bracket. 9. Assign the highest income for tax bracket 2 to be $40K and the tax percent to 20%. Anything over $40K is in the next tax bracket. 10. Ask the user to enter a dollar amount. 11. Convert the data entered into a number.

Logic and Output section 12. Use only variables in your logic. 13. Determine whether or not the dollar amount entered is taxable. 14. Determine whether or not the dollar amount is in tax bracket 1 or 2. 15. Calculate the amount of tax on the dollar amount and display a message that tells the user what the tax amount would be on the number they entered. 16. For amounts greater than $40k display the message “I do not have the data to calculate the tax on this income.

Testing: Try values that are equal to the highest income for each bracket and the highest income for no taxes. Try numbers greater than the 40,000. Try amounts like 25,001 or 40,001.

My code thus far:

<script type="text/javascript">

        // variable declarations

            var userIncome;
            var minIncomeNoTax;
            var taxPercentBrack1;
            var taxPercentBrack2;
            var hiIncomeBrack1;
            var hiIncomeBrack2;
            var currentTaxBracket;

       // Assignments

            userIncome = prompt("Please enter your income in dollar amount.","");
            minIncomeNoTax = 1500;
            taxPercentBrack1 = 15/100;
            taxPercentBrack2 = 20/100;
            hiIncomeBrack1 = 25000;
            hiIncomeBrack2 = 40000;

        // Calculations & Output

            if (userIncome >=minIncomeNoTax && userIncome <=hiIncomeBrack2)
            {
                alert("Your income is taxable.");
            }
            else if (userIncome >=minIncomeNoTax && userIncome <=hiIncomeBrack1)
            {
                alert("Your income amount is in tax bracket 1.");
            }
            else if (userIncome >hiIncomeBrack1 && userIncome <=hiIncomeBrack2)
            {
                alert("Your income amount is in tax bracket 2.");
            }
            else
            { 
                alert("Sorry, I do not have the data to calculate the tax on this income.");
            }


            // output

            document.write("Your Income: &nbsp; $" +userIncome + "<br />");


    </script>
7
  • You've haven't precisely described your problem. Go back and do that. All I see is a block of code, a line that says "...there's an error...", and the (now removed) homework assignment details. Commented Feb 6, 2012 at 2:53
  • Your assignment sounds like it was written by someone who does not know what they are teaching or how to teach it... Commented Feb 6, 2012 at 2:59
  • I'm working on picking apart all the issues in this, and setting up something that works. give me a few. Commented Feb 6, 2012 at 3:21
  • thanks 4 the help and effort!! Commented Feb 6, 2012 at 3:24
  • There's a lot wrong, a lot of stuff Jonathan hasn't picked up yet. I'm writing some comments into your code with the fixes, and I'll post an answer in a couple minutes. Commented Feb 6, 2012 at 3:29

1 Answer 1

2

I fixed your if/else statement, and it seems to work now. I put it on jsfiddle:

http://jsfiddle.net/gXQXG/13/

Your issue was

if (userIncome <=1500 && userIncome >=40000)

else if (userIncome <=1500 && userIncome >=25000)

else if (userIncome <=25001 && userIncome >=40000)

The second statement in all three should be <=

A number cannot be both less than 1500 and greater than 4000 ;)

Next Step

You should replace the constants 1500, 25000, and 40000 with the variables you declared, hiIncomeBrack1 and hiIncomeBrack2

Lastly, there is one more issue in your logic, but, I will let you find that one. It has to do with two of the <= needing to actually be a <

Updated Code

// variable declarations
var userIncome;
var minIncomeNoTax;
var taxPercentBrack1;
var taxPercentBrack2;
var hiIncomeBrack1;
var hiIncomeBrack2;
var currentTaxBracket;
var totalTaxDue;

// Assignments
userIncome = prompt("Please enter your income in dollar amount.", 0);
minIncomeNoTax = 1500;
taxPercentBrack1 = 15 / 100;
taxPercentBrack2 = 20 / 100;
hiIncomeBrack1 = 25000;
hiIncomeBrack2 = 40000;

// Calculations & Output
if (userIncome >= minIncomeNoTax && userIncome <= hiIncomeBrack2)
{   //The user's income falls within our range of knowledge.
    alert("Your income is taxable.");

    if (userIncome >= minIncomeNoTax && userIncome < hiIncomeBrack1)
    {   //The user falls into our first bracket
        alert("Your income amount is in tax bracket 1.");
        currentTaxBracket = taxPercentBrack1;
    }
    else if (userIncome >= hiIncomeBrack1 && userIncome <= hiIncomeBrack2)
    {   //The user falls into our second bracket
        alert("Your income amount is in tax bracket 2.");
        currentTaxBracket = taxPercentBrack2;
    }
}
else
{   //Can't help this user, they are not within our limits.
    alert("Sorry, I do not have the data to calculate the tax on this income.");
}


//Figure out the actual amount due
//Need to use parseInt to convert from string to int.(User types a string into the prompt.)
totalTaxDue = currentTaxBracket * parseInt(userIncome);

// output
document.write("Your Income: &nbsp; $" + userIncome + "<br />");
//Multiply the decimal tax rate by 100 so we can print out a nice and clean %.
document.write("Your Tax Percent: &nbsp; " + (currentTaxBracket * 100) + "%<br />");
document.write("Pay Uncle Sam: &nbsp; $" + totalTaxDue + "<br />");
Sign up to request clarification or add additional context in comments.

13 Comments

ok, awesome. i fixed that error. now can you help with figuring out the part with writing to the document their tax amount?
i'm confused..sorry this is only my third week of class. :(
@StephanieKay to find their actual tax amount, you need to know which tax bracket they are in. I would declare another variable, currentTaxBracket at the top of the script with the others. Then, in your first and second else ifs, add currentTaxBracket = taxPercentBrak1; (Obviously, in the second else if change it from taxPercentBrak1 to taxPercentBrak2). Now, when your code finished executing the ifs, you will have the tax percentage stored in currentTaxBracket You will then just multiple currentTaxBracket by their income and put the return value in the document.write()
@StephanieKay Uh... Was that follow-able?
@StephanieKay We could fix that by adding a regular expression search-replace: replace(/[^\d.]/g, ""); but I feel like your teacher might think you just copied that from somewhere if they are not teaching it in class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.