Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Creating code with If...else if...else Statement
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-27-2011, 10: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 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.
 11-27-2011, 10:28 AM PM User | #2 Amphiluke Regular Coder     Join Date: Jul 2009 Posts: 312 Thanks: 3 Thanked 89 Times in 89 Posts 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
 Users who have thanked Amphiluke for this post: promo786 (11-27-2011)
 11-27-2011, 10:39 AM PM User | #3 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,463 Thanks: 200 Thanked 2,468 Times in 2,446 Posts 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 10:48 AM..
 Users who have thanked Philip M for this post: promo786 (11-27-2011)
11-27-2011, 10:52 AM   PM User | #4
Amphiluke
Regular Coder

Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
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

11-27-2011, 11:00 AM   PM User | #5
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,463
Thanks: 200
Thanked 2,468 Times in 2,446 Posts
Quote:
 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.
__________________

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.

 11-27-2011, 11:02 AM PM User | #6 Amphiluke Regular Coder     Join Date: Jul 2009 Posts: 312 Thanks: 3 Thanked 89 Times in 89 Posts I think, this is JavaScript basics, which must be learned first. __________________ I am still learning English
11-27-2011, 11:08 AM   PM User | #7
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,463
Thanks: 200
Thanked 2,468 Times in 2,446 Posts
Quote:
 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!
__________________

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.

 11-27-2011, 06: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 Thanks to all the responses. It really helps me to get the idea and to start and proceed with my code writing.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:10 AM.