Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    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.

  • #2
    Regular Coder Amphiluke's Avatar
    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)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 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?
    Last edited by Philip M; 11-27-2011 at 09:48 AM.

    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.

  • Users who have thanked Philip M for this post:

    promo786 (11-27-2011)

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #6
    Regular Coder Amphiluke's Avatar
    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #8
    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.


  •  

    Posting Permissions

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