Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2011, 09:15 AM   PM User | #1
promo786
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
promo786 is an unknown quantity at this point
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.
promo786 is offline   Reply With Quote
Old 11-27-2011, 09:28 AM   PM User | #2
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
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;
__________________
I am still learning English
Amphiluke is offline   Reply With Quote
Users who have thanked Amphiluke for this post:
promo786 (11-27-2011)
Old 11-27-2011, 09:39 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-27-2011 at 09:48 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
promo786 (11-27-2011)
Old 11-27-2011, 09:52 AM   PM User | #4
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
Quote:
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.
__________________
I am still learning English
Amphiluke is offline   Reply With Quote
Old 11-27-2011, 10:00 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Amphiluke View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-27-2011, 10:02 AM   PM User | #6
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
I think, this is JavaScript basics, which must be learned first.
__________________
I am still learning English
Amphiluke is offline   Reply With Quote
Old 11-27-2011, 10:08 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Amphiluke View Post
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!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-27-2011, 05:07 PM   PM User | #8
promo786
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
promo786 is an unknown quantity at this point
Thanks to all the responses.

It really helps me to get the idea and to start and proceed with my code writing.
promo786 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 PM.


Advertisement
Log in to turn off these ads.