JavaScript
Programming
The hardest part about programming is figuring out
how to get the computer to do what you want it to do. Once you determine how you would solve a task, you must then
specify statements in the computer's programming language that would do the
same thing. The problem here is that
you must specify EVERYTHING down to the smallest detail to the computer. Humans are smarter than computers, because
we can make and understand assumptions about the smaller details.
Lab Assignment
This week, we are going to create a very simple income tax form.
The form will have two text fields.
The first one is where the user will enter their total income. The second field is where the computed
income tax is displayed. The form will
also need one button. When this button
is pressed, the income tax is computed for the income entered in the first
field.
The income tax will be a graduated tax, similar to
what is used by the federal government.
We will use the following tax information:
|
Income Range |
Tax |
|
$0 - $2,000 |
Nothing |
|
$2,001 - $20,000 |
4% * Each dollar earned over $2,000 |
|
$20,001 - $40,000 |
$720 + 8% * Each dollar earned over $20,000 |
|
$40,001 - $80,000 |
$2,320 + 16% * Each dollar earned over $40,000 |
|
$80,001 - $120,000 |
$8,720 + 24% * Each dollar earned over $80,000 |
|
$120,001 + |
$18,320 + 32% * Each dollar earned over $120,000 |
You will want to use the JavaScript if statement
to have your code determine which tax bracket the user falls into.
IF Statement
The if statement allows other statement(s)
to be conditionally executed. This
means these others statement(s) may or may not get executed depending on the
result of a conditional expression. The
form of an if statement is:
if ( cond )
statement
where: |
if |
a keyword of the language |
|
cond |
the conditional expression which will be
evaluated to either true or false |
|
statement |
the other statement(s) that will get execute if
the conditional expression evaluates to true. |
Conditional Expressions
The conditional expression will normally make use
of the relational and boolean operators.
The relational operators are:
1. |
== |
equal to |
2. |
!= |
not equal to |
3. |
< |
less than |
4. |
<= |
less than or equal to |
5. |
> |
greater than |
6. |
>= |
greater than or equal to |
The boolean operators are:
1. |
&& |
and |
2. |
|| |
or |
3. |
! |
not |
Block of Statements
The other statement(s) must be a single statement
or a block of statements. The block of
statements is a group or zero or more statements enclosed by the curly brace
characters: { and }.
IF - ELSE Statement
There is a variation on the if statement. This is the if-else statement. The if-else statement has two sets of other
statement(s). The first set is executed
if the conditional expression evaluates to true. The second set is executed if the conditional expression
evaluates to false. The first set of
other statements is normally called the "if clause" or sometimes the
"then clause" of the if statement.
The second set of other statements is normally called the "else
clause" of the if statement. The
form of the if-else statement is:
if ( cond )
statement1
else
statement2
where: |
if |
a keyword of the language |
|
cond |
the conditional expression which will be
evaluated to either true or false |
|
statement1 |
the other statement(s) that will get execute if
the conditional expression evaluates to true, the "if clause" |
|
else |
a keyword of the language |
|
statement2 |
the other statement(s) that will get execute if
the conditional expression evaluates to false, the "else clause" |
Nested IF Statements
Often a program will need to check for a number of
conditions. This can be done by using
nested if statements, which are nothing more than multiple IF-ELSE statements. The else clauses in a Nested if statement
always contain either another IF-ELSE statement. The last IF-ELSE statement is sometimes just an if statement. The form is:
if ( cond1 )
statement1
else if ( cond2 )
statement2
else if ( cond3 )
statement3
...
else if ( condN )
statementN
else
statementN+1