Web Analytics Made Easy -
StatCounter Conditionals - CodingForum

Announcement

Collapse
No announcement yet.

Conditionals

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Conditionals

    Please help me! I am brand new and I don't even know where to start. I need to write a code that will break minors down to 4 categories: <=5, go to kindergarten; <=13 for take recess; 13 to 17 for teenagers; and >=16 for getting a drivers license. I've made a bunch of false starts but can't get to the first line! I get boggled trying to think it through because many of the categories overlap. Each child or parent should be prompted to enter the child's age. When the child's age is entered, the script should do a document.write that says something like: You are (age) so you should (whatever).
    For instance, if 13 is entered for the age then an
    heading should write out: You are 13 so you should take recess.
    Here is as far as I can get:

    <html>
    <head>
    <title>ages</title>
    </head>
    <script language="JavaScript" type="text/javascript"><!--
    var visitor = prompt("Please enter your age.", "")
    var age = "1"
    if (age <= 5)
    {
    document.write("
    You are ' So You Should:
    ")
    }
    //-->
    </script>
    </body>
    </html>

    I know it's not much, but I'm totally confused. Please help me. Thanks!

  • #2
    You must solve the overlap conflict first. What message would you say if the visitor is 16 or 17 years old?
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      >=16 would be something like: "you are (17) so you can (drive).

      Comment


      • #4
        What about "13 to 17 for teenagers"?
        and 18-19, are they not teenagers?
        and 13 who are teenagers and can take recess?
        and >=16 who are teenages and can drive?

        Can you tell us what exactly the messages are for those scenarios?
        Last edited by glenngv; Mar 3, 2004, 12:06 AM.
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          Well---What I was thinking was that multiple statements would write if more than one is true. Is that not possible?

          Comment


          • #6
            I edited my post while you were posting a reply.
            Ok, combine them. One last question left unanswered.
            What about 18 and 19, are they not teenagers?
            Glenn
            vBulletin Mods That Rock!

            Comment


            • #7
              Ok, I considered 18-19 as teenagers as it should.

              Code:
              <script type="text/javascript">
              var age = parseInt(prompt("Age?",""),10);
              var str='';
              
              if (age>=16 && age<=19) str=" and you can drive";
              else if (age>=20) str=" can drive";
              
              if (age>=13 && age<=19) str=" are a teenager"+str;
              
              if (age==13) str+=" and you can take recess";
              else if (age>5 && age<13) str+=" can take recess";
              else if (age<=5 && age>0) str=" go to kindergarten";
              else if (age==0) str=" are still a baby";
              else if (isNaN(age) || age<0){
                age="(invalid age)"
                str=" are ???";
              }
              document.write("You are "+age+" so you"+str+".");
              </script>
              Glenn
              vBulletin Mods That Rock!

              Comment


              • #8
                Hi! Sorry! I didn't intend to bail out while you were assisting me last nite, but it was after 1 a.m. I had been up since 4 a.m. and couldn't hold my eyes open any longer. I just checked the forum and found your reply. After breakfast, etc., I'll work with it and let you know how it comes out later. THANK YOU for helping!

                Comment


                • #9
                  treepixie wrote on 03-04-2004 01:55 AM:
                  HI! Me again! I made a few corrections and have it running but it doesn't report correctly. Seems to be those overlaps in ages. any suggestions would be greatly appreciated!! Thanks!

                  <html>
                  <head>
                  <title>age</title>
                  </head>
                  <body>
                  <script language="JavaScript" type="text/javascript"><!--
                  var age = parseInt(prompt("Please enter your age.", ""))
                  var state = ""

                  if (age >= 13 && age <= 19)
                  { state=" be a teenager."
                  }
                  else if (age < 18)
                  { state=" report to your parents."
                  }
                  else if (age >= 16)
                  { state=" drive."
                  }
                  else if (age <= 13)
                  { state=" be a child."
                  }
                  else if (age <5)
                  { state=" take a nap every day."
                  }

                  document.write("<h1>Being " + age + " Entitles You To: " + state + ".</h1>")

                  //-->
                  </script>
                  </body>
                  </html>
                  Here's the code.
                  Code:
                  <html>
                  <head>
                  <title>age</title>
                  </head>
                  <body>
                  <script language="JavaScript" type="text/javascript">
                  <!--
                     var age = parseInt(prompt("Please enter your age.", ""));
                     var state = "";
                  
                     //teenager
                     if (age >= 13 && age <= 19)
                     { 
                       state = " be a teenager";
                     }
                  
                     //teenager also, combine with previous state.
                     if (age < 18)
                     { 
                       state += " and report to your parents";
                     }
                  
                     //not teenager anymore, not combined with any state
                     if (age >= 20)
                     { 
                       state = " drive";
                     }
                     else if (age >= 16) //teenager also, combine with previous state.
                     { 
                       state += " and drive";
                     }
                     
                     if (age <5)
                     { 
                       state = " take a nap every day";
                     }
                     else if (age <= 12)
                     { 
                       state = " be a child";
                     }
                     else if (isNaN(age) || age < 0) //input error
                     { 
                       state=" nothing";
                       age="(unknown age)";
                     }
                   
                   document.write("<h1>Being " + age + " Entitles You To: " + state + ".</h1>")
                  
                  //-->
                  </script>
                  </body>
                  </html>
                  Glenn
                  vBulletin Mods That Rock!

                  Comment


                  • #10
                    Ok, let's get this thing organized! If this code is for a serious application then it will need some re-work. Nothing personal guys, but if other parts of the application need to know the various "states" of a minor then we need some functionALITY here.

                    And as far as I can tell, this will help resolve the "overlap" problem because we can use these functions in any combination to determine the "complete state" of a minor; or any "sub-state" for that matter.

                    Oh, and of course I made this an object. That just seemed to be the thing to do at the time.

                    This may seem like overkill , but it solves some problems and prevents future ones. There may seem to be a lot of code here, but take a good look... the functions are short and simple. Therefore easy to understand, easier to modify, easier to debug, etc.

                    Notice how much more simplified the "driver" code becomes in the <body>.

                    Notice that if the definition of what a "teenager" is, or "driving age" or any other state, then we only make a change in one small function. No other code anywhere changes, not even the driver code (i.e. code in the <body>)!

                    Oh, and notice that the "age" variable in the <body> DOES NOT conflict with the "age" property / variable in the Minor object. Constructing an Object like this encapsulates all the variables and functions so they DO NOT conflict with any same-name things anywhere else on the same html page.
                    Code:
                    <html>
                    <head>
                    <title>age</title>
                    <script type="text/javascript">
                    function Minor (age, name) {
                       if (arguments[0] && !isNaN(age)) {
                    	    this.age = age;
                    	 }else{
                    	    this.age = 0;
                    	 }
                    			
                       if (arguments[1]) {
                          this.name = name;
                       }else{
                          this.name = "Bubba"
                       }
                    	 
                       this.state = new String();
                    	 
                       // define the various states
                    	 
                        // it may seem like I'm making for excessive typing in my
                        // functions below, but don't these "constants" nicely
                        // document things?
                        this.TEENAGE_MIN = 13;
                        this.TEENAGE_MAX = 17;
                        this.DRIVING_AGE = 16;
                        this.GOTO_SCHOOL_AGE = 5;
                        this.MINOR_AGE_MAX = 19;
                    
                        // define the methods
                       this.isTeenager = function () {
                          var isTeen = false;
                    
                          if (this.age >= this.TEENAGE_MIN && this.age <= this.TEENAGE_MAX) {
                             isTeen = true;
                          }else{
                             isTeen = false;
                          }
                          return isTeen;
                       }
                    
                       this.inKindergarten = function () {
                          var inPreschool = false;
                    
                          if (this.age <= this.GOTO_SCHOOL_AGE) {
                             inPreschool = true;
                          }else{
                             inPreschool = false;
                          }
                          return inPreschool;
                       }
                    
                       this.drivingAge = function () {
                          var canDrive = false;
                    
                          if (this.age >= this.DRIVING_AGE) {
                             canDrive = true;
                          }else{
                             canDrive = false;
                          }
                          return canDrive;
                       }
                    
                    // you get the idea....
                    
                       this.determineStatus = function() {
                    
                          if (this.inKindergarten()) {
                             this.status += "\nIs in Kindergarten";
                          }else{
                             this.status += "\ntoo old to attend Kindergarten";
                          }
                    
                          if (this.canDrive()) {
                             this.status += "\nis of driving age";
                          }else{
                              this.status += "\n Cannot drive yet";
                    
                          // you get the idea ...
                    	 
                       }// determinStatus()}
                    
                    this.determineStatus() // status determined during Object construction
                    }  // end of minor object
                    
                    </script>
                    </head>
                    <body>
                    <script language="JavaScript" type="text/javascript">
                       var age = parseInt(prompt("Please enter your age.", ""));
                       var someKid = new Minor (age);  // name should default to "bubba"
                    
                       document.write "<h1>Being " + someKid.age + " Entitles You To:</h1>" + someKid.status;
                    </script
                    </body>
                    </html>
                    Last edited by RadarBob; Mar 4, 2004, 12:48 PM.

                    Comment


                    • #11
                      Nice approach!

                      But my solution tried to use the exact words wanted by the original poster and doing it in a complete-sentence-like way especially when combining overlap ages. And my script not only covered minors but also those above 20s.

                      But of course, your code can be easily modified to apply those requirements.
                      Glenn
                      vBulletin Mods That Rock!

                      Comment

                      Working...
                      X