www.webdeveloper.com
Results 1 to 7 of 7

Thread: If else function

  1. #1
    Join Date
    May 2012
    Posts
    3

    Post If else function

    Hello, guys. I'm a new student in programming class. We're studying "if else functions" right now, and i've got a project to turn in soon. The objective is to be able to tell where i'm at, at a given time. Please look at my codes and help me in correcting it.

    <html>
    <head>
    <font size= "100" font color= "green"> Where is Mishaal? </font>
    <title>Mishaal Locator</title>
    <body>

    <script type= "text/javascript">

    var d= new Date() ;
    var time= d.getHours() ;
    var minute= d.getMinutes() ;

    if (time<=7 && minute>=35)
    {
    document.write("<b>Mishaal is in Fitness class</b>");
    }
    else if (time>= 8 && minute>=15)
    {
    document.write("<b> Mishaal is in the hall</b>");
    }
    if (time<=8 && minute>=20)
    {
    document.write("<b> Mishaal is in Arabic Class</b>");
    }
    else if (time>=9 && minute>=40)
    {
    document.write("<b> Mishaal is in the hall</b>");
    }
    if (time<=9 && minute>=50)
    {
    document.write("<b> Mishaal is in Biology Class </b>");
    }
    else if ( time>=11 && minute>=10)
    {
    document.write("<b> Mishaal is out for Lunch</b>");
    }
    if (time<=11 && minute>=50)
    document.write("<b> Mishaal is in Spanish Class</b>");
    {
    else if (time>=13 && minute>=5)
    document.write("<b> Mishaal is in the hall</b>");
    }
    if (time<=13 && minute>=15)
    document.write("<b> Mishaal is in Programming</b>");
    {
    else (time>=14 && minute>=30)
    document.write("<b> Mishaal's going home</b>");
    }

    </script>
    </body>
    </html>

    thank you for your time.

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    You probably want to keep saying "else if" instead of rotating every other statement.

    Ex:

    Code:
    if (time<=7 && minute>=35)
    {
    document.write("<b>Mishaal is in Fitness class</b>");
    }
    else if (time>= 8 && minute>=15)
    {
    document.write("<b> Mishaal is in the hall</b>"); 
    }
    else if (time<=8 && minute>=20)
    {
    document.write("<b> Mishaal is in Arabic Class</b>");
    }
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    Also, you want to put a catch all at the end to account for any time in between your classes.

    Code:
    else if (time<=13 && minute>=15)
    document.write("<b> Mishaal is in Programming</b>"); 
    {
    else if (time>=14 && minute>=30)
    document.write("<b> Mishaal's going home</b>");
    } else {
    document.write('<b>Mishaal is in between classes.</b>');
    }
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  4. #4
    Join Date
    May 2012
    Posts
    3
    thank you for your help, i really appreciate it. If i may, i just have one more question to ask. If i were to do this schedule for two days, with different classes would this method work properly?

  5. #5
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    You'll have to implement another check to see what day of the week it is.

    http://www.w3schools.com/jsref/jsref_getday.asp
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  6. #6
    Join Date
    Mar 2012
    Posts
    14
    Do yourself a favor and preview all of your javascript in a syntax checker. There is one here: http://jslint.com/

    It will most likely hurt your feelings at first. But it is great for someone learning the language. Make sure to read the instructions.

  7. #7
    Join Date
    May 2012
    Posts
    3
    thank you for all the help, i really appreciate it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles