user3733575
user3733575

Reputation: 53

can't find error in this javascript code

it's not even running. Something is wrong with the do while loop. I'm assuming it's something with all the if statements? If someone could help me out i'd greatly appreciate it. Thanks.

        do {
        entry = prompt("Enter taxable income as a valid number\n" +
                       "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

        // calculate the tax owed here
        if (entry < 0){
            document.write("Please enter a positive number");
        }
        if (entry > 0 && entry < 8701){
            tax_owed = entry * 0.10;
        }
        if (entry > 8700 && entry < 35351){
            tax_owed = 870 * 0.15;
            tax_owed += entry;
        }
        if (entry > 35350 && entry < 85651){
            tax_owed = 4867 * 0.25;
            tax_owed += entry;
        }
        if (entry > 85650 && < 178651){
            tax_owed = 17442 * 0.28;
            tax_owed += entry;
        }
        if (entry > 178650 && entry < 388351){
            tax_owed = 43482 * 0.33;
            tax_owed += entry;
        }
        if (entry > 388350){
            tax_owed = 112683 * 0.35;
            tax_owed += entry;
        }

        alert("Tax owed is " + tax_owed);
    }
    while (entry != 99999);

Upvotes: 2

Views: 145

Answers (4)

DenisFLASH
DenisFLASH

Reputation: 742

The error of missing operand before < 178651 is already found, but in case you want your code to have less potential errors, i suggest a light refactoring procedure.

  • Using else if will be more efficient. Once found a block that corresponds to a given entry value, the program will stop checking other conditions. Moreover, you will have to specify just those values which divides your range (your "milestones"), instead of repeating them like entry < 8701 followed by entry > 8700. Finally, it wil fix other bug which is printing tax_owed = ..something like 140000.. when you enter 99999.

  • Changing the condition of the loop (introducing a boolean variable) would be more readable.

  • I'm not a tax man at all, but are you sure you want to add tax_owed += entry; ? I mean, do i really owe $10130.5 when declaring $10000? In any case, it will be more readable to have 1 line instead of 2: tax_owed = entry + 17442 * 0.28; // but do you really add 'entry'?.

So, the code will be something like this:

continueEntering = true;

while ( continueEntering ) {
    tax_owed = 0;
    entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

    if (entry == 99999) {
        continueEntering = false;
    } else if (entry < 0){
        alert("Please enter a positive number"); 
    } else if (entry <= 8700){
        tax_owed = entry * 0.10;
    } else if (entry <= 35350){
        tax_owed = 870 * 0.15 + entry;
    } else if (entry <= 85650){
        tax_owed = 4867 * 0.25 + entry;
    } else if (entry <= 178650){
        tax_owed = 17442 * 0.28 + entry;
    } else if (entry <= 388350){
        tax_owed = 43482 * 0.33 + entry;
    } else {
        tax_owed = 112683 * 0.35 + entry;
    }

    alert("Tax owed is " + tax_owed);
}

As said in comments, it's really better not to use the values themselves inside the body of your loop. It will be way easier to change one day those values, if they are stored in variables. here you can use 2 arrays. You can call them, say, thresholds[] and tax_percentage[], or whatever (you know it better than me). The good thing of using arrays is that you'll be able to replace the sequence of if statements by just one for loop inside your original while loop.

================ UPDATE ====================

Here is how you can refactor the above code to use a for loop instead of lots of if.

continueEntering = true;

while ( continueEntering ) {
    tax_owed = 0;
    exitValue = 99999;

    tax_threshold = [ 0, 8700, 35350, 85650, 178650, 388350 ];
    tax_rate = [ 0.10, 0.15, 0.25, 0.28, 0.33, 0.35 ];
    additional_tax = [ 0, 870, 4867, 17442, 43482, 112683 ];

    entry = prompt("Enter taxable income as a valid number\n" + 
        "Or enter " + exitValue + " to end entries", exitValue);
    entry = parseInt(entry);

    if (entry == exitValue) {
        continueEntering = false;
    } else if (entry < 0){
        alert("Please enter a positive number"); 
    } else {

        for( i = tax_threshold.length-1; i >= 0; i-- ) {
            if ( entry > tax_threshold[i] ) {
                tax_owed = entry - tax_threshold[i];
                tax_owed *= tax_rate[i];
                tax_owed += additional_tax[i];
                break;
            }
        }
    }

    alert("Tax owed is " + tax_owed.toFixed(2));
}

Upvotes: 0

user3733575
user3733575

Reputation: 53

Ok I figured out the proper to way calculate the taxes. You guys were right. I was way off. I'm using the 2012 income tax policy because that's what my professor wanted us to use. Thanks everyone for the help again. I'm really liking this site! Great for us n00bs.

    var tax_owed = 0;
    var entry;

    continueEntering = true;

    while (continueEntering) {
        tax_owed = 0;
        entry = prompt("Enter taxable income as a valid number\n" + "Or enter 99999 to end entries", 99999);
        entry = parseInt(entry);

        if (entry == 99999) {
            continueEntering = false;
        } else if (entry < 0){
            alert("Please enter a positive number"); 
        } else if (entry <= 8700){
            tax_owed = entry * 0.10;
        } else if (entry <= 35350){
            tax_owed = entry - 8700;
            tax_owed *= 0.15;
            tax_owed += 870;
        } else if (entry <= 85650){
            tax_owed = entry - 35350;
            tax_owed *= 0.25;
            tax_owed += 4867;
        } else if (entry <= 178650){
            tax_owed = entry - 85650;
            tax_owed *= 0.28;
            tax_owed += 17442;
        } else if (entry <= 388350){
            tax_owed = entry - 178650;
            tax_owed *= 0.33;
            tax_owed += 43482;
        } else if (entry > 388350){
            tax_owed = entry - 388350;
            tax_owed *= 0.35;
            tax_owed += 112683;
        }

        alert("Tax owed is " + tax_owed.toFixed(2));

}

Upvotes: 0

Georgi-it
Georgi-it

Reputation: 3686

On the line if (entry > 85650 && < 178651) if you look closely will see that you are not comparing the second number to anything. It should be if (entry > 85650 && entry < 178651)

Upvotes: 1

Mritunjay
Mritunjay

Reputation: 25882

In your 21 line

if (entry > 85650 && < 178651){ // there is no operand between && and <.

I think it should be

if (entry > 85650 && entry< 178651){

Upvotes: 1

Related Questions