Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. How to shorten/combine "if()" statements?

How to shorten/combine "if()" statements?

Scheduled Pinned Locked Moved JavaScript
javascripttutorialquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11816233
    wrote on last edited by
    #1

    Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.

    M G 2 Replies Last reply
    0
    • U User 11816233

      Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.

      M Offline
      M Offline
      Member 11850450
      wrote on last edited by
      #2

      use case/switch statement

      switch (true) {
      case (age == 21):
      alert(" equal");
      break;
      case (age> 21):
      alert(" older");
      break;
      case (age < 21):
      alert("younger");
      break;}

      1 Reply Last reply
      0
      • U User 11816233

        Is there a way to shorten or combine if() statements? Currently I have if age<21 there is an alert, then else if age>21 there is an alert, another else if age==21 alert, and an else alert. Is there a way to combine age<21, age>21, age==21 into one statement with their individual alerts? Thanks, I am taking my first Javascript class right now.

        G Offline
        G Offline
        Godhaniketan
        wrote on last edited by
        #3

        <script>
        function myfun() {
        var age = 21; //change value of age and check it.
        (age < 21) ? alert('less than 21') : (age > 21) ? alert('greater than 21') : (age == 21) ? alert('equl 21') : alert('else part');
        }
        </script>

        It's give you result 100% with single line. :)

        L 1 Reply Last reply
        0
        • G Godhaniketan

          <script>
          function myfun() {
          var age = 21; //change value of age and check it.
          (age < 21) ? alert('less than 21') : (age > 21) ? alert('greater than 21') : (age == 21) ? alert('equl 21') : alert('else part');
          }
          </script>

          It's give you result 100% with single line. :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The third expression "age == 21" is redundant. If it is not less than and not greater than then there is only one other value it can have.

          G 1 Reply Last reply
          0
          • L Lost User

            The third expression "age == 21" is redundant. If it is not less than and not greater than then there is only one other value it can have.

            G Offline
            G Offline
            Godhaniketan
            wrote on last edited by
            #5

            Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.

            L 1 Reply Last reply
            0
            • G Godhaniketan

              Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Godhaniketan wrote:

              Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.

              Well I guess you do not understand that simple statement only requires two if parts. If age is less than 21 : Alert1 If age is greater than 21 : Alert2 Else: age must be equal to 21, so the test for equality is not needed.

              G 1 Reply Last reply
              0
              • L Lost User

                Godhaniketan wrote:

                Hello Richard MacCutchan, I think you have not tested well in my case it's worked fine. i think you should have to check again. if any doubt then reply me. just change value of age like 19,21,22 and 'blah..' it gives you all kind of alert..please try it out.

                Well I guess you do not understand that simple statement only requires two if parts. If age is less than 21 : Alert1 If age is greater than 21 : Alert2 Else: age must be equal to 21, so the test for equality is not needed.

                G Offline
                G Offline
                Godhaniketan
                wrote on last edited by
                #7

                Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.

                L 1 Reply Last reply
                0
                • G Godhaniketan

                  Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Godhaniketan wrote:

                  Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.

                  Yes, and that is what my logic provided. If you really cannot understand simple boolean expressions then ...

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    Godhaniketan wrote:

                    Hello Richard MacCutchan, have you read question of "Member 11849688" he written clearly that "else if age==21 alert" that means he/she wants alert on equality condition also. please read out the question properly. he/she want 4 alert <, >, ==, else part.

                    Yes, and that is what my logic provided. If you really cannot understand simple boolean expressions then ...

                    G Offline
                    G Offline
                    Godhaniketan
                    wrote on last edited by
                    #9

                    Yes i understand your logic.. i will remove redundancy. thanks

                    L 1 Reply Last reply
                    0
                    • G Godhaniketan

                      Yes i understand your logic.. i will remove redundancy. thanks

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      And in future, please do not remove messages as you have done here. We all get things wrong from time to time (me more often), so just be adult enough to accept correction.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups