Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Creating code with If...else if...else Statement

1. ## Creating code with If...else if...else Statement

Hi, I am new here without any coding skills but willing to learn. I know how
to use html code. I am thinking of how to write the code for below scenario
to create a simple online customize calculator:

There is 1 box which allow us to enter any number=x (representing amount of money).
So whenever we entered a number in the box and click "CALCULATE" buton below the box,
there will be 3 results generated in 3 boxes below it based on the set of of rules
i.e.

1. if the amount entered is <21,000

Result 1 = 1.5%*x*12
Result 2 = 1.5%*x*48
Result 3 = 1.5%*x*120

2. if the amount entered is >=21,000 and <210,000

Result 1 = 1.8%*x*12
Result 2 = 1.8%*x*48
Result 3 = 1.8%*x*120

3. if the amount entered is >=210,000

Result 1 = 2.2%*x*12
Result 2 = 2.2%*x*48
Result 3 = 2.2%*x*120

I understand that this code will involve If...else if...else Statement..

Anyone can give me any references/examples similar to this scenario? Thanks.

• Code:
```var pt = (x < 21000) ? 1.5 : ((x >= 210000) ? 2.2 : 1.8),
result1 = pt * x * 12,
result2 = pt * x * 48,
result3 = pt * x * 120;```

• ## Users who have thanked Amphiluke for this post:

promo786 (11-27-2011)

• Amphiluke-

He is a total beginner and wants simple if..else statements, not a ternary operator!! And your code has syntax errors (, instead of ; at end of line)

Code:
```var pt = 1.5;
if (x >= 21000) {pt = 1.8}
if (x >= 210000) {pt = 2.2}
pt = pt/100;  // percentage

result1 = pt * x * 12;
result2 = pt * x * 48;
result3 = pt * x * 120;```
You may wish to show the results to (say) two places of decimals by using .toFixed(2). e.g. result1 = (pt*x*12).toFixed(2);

Quizmaster: Where was Napoleon Bonaparte born?
Contestant: Was it England?

• ## Users who have thanked Philip M for this post:

promo786 (11-27-2011)

• And your code has syntax errors
Sorry, but you are wrong. It's not an error. Variables may be separated with commas when initialized by the var operator.

• Originally Posted by Amphiluke
Sorry, but you are wrong. It's not an error. Variables may be separated with commas when initialized by the var operator.
Hmm. You are indeed right, but that is not something to offer to a beginner, especially without any explanation. Semi-colons would have worked just as well.

• I think, this is JavaScript basics, which must be learned first.

• Originally Posted by Amphiluke
I think, this is JavaScript basics, which must be learned first.
For myself, I see that is likely to cause confusion. I might perhaps write var x,y,z; but I would not do that if the variable was initialised with an expression such as you are using. I cannot see any advantage in that. But each to his taste!

• Thanks to all the responses.

It really helps me to get the idea and to start and proceed with my code writing.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•