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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy If statement not working properly

    Hello There!
    I am fairly new to javascript and am in need of some help. I am trying to make a simple script that tells you what class period it is. I've gotten everything working except for the If statement that tells you what period it is. I can't figure out if i'm doing something wrong, and it's starting to drive me insane. Here's the code for the script. I've tried to comment it as much as possible to make it easy to understand.
    Code:
    //define variables
    //Get current time
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    //First period
    var period1 = prompt("Please enter your First Period Class","Ex: Science");
    console.log ("First period is: " +period1+ "." );
    //Second Period
    var period2 = prompt("Please enter your Second Period Class","Ex: PE");
    console.log ("Second period is: " +period2+ "." );
    //Third Period
    var period3 = prompt("Please enter your Third Period Class","Ex: Language Arts");
    console.log ("Third period is: " +period3+ "." );
    //Fourth Period
    var period4 = prompt("Please enter your Fourth Period Class","Ex: Math");
    console.log ("Fourth period is: " +period4+ "." );
    //Fifth Period
    var period5 = prompt("Please enter your Fifth Period Class","Ex: MSI");
    console.log ("Fifth period is: " +period5+ "." );
    //Sixth Period
    var period6 = prompt("Please enter your Sixth Period Class","Ex: Band");
    console.log ("Sixth period is: " +period6+ "." );
    //Seventh period
    var period7 = prompt("Please enter your Seventh Period Class","Ex: History");
    console.log ("Seventh period is: " +period7+ "." );
    //If minutes are less than 10 add zero before number
    if (minutes < 10){
    minutes = "0" + minutes;
    }
    //Add a blank Line
    console.log ();
    //Show Current time
    console.log ("Current time is " + hours + ":" + minutes);
    //Define variable time
    var time = (hours + "." + minutes);
    //Before School
    if (time >= 0.01 & time <= 7.40)
    console.log ("School hasn't started yet.");
    //After School
    if (time >= 3.20 & time <= 0.00)
    console.log ("School is over.");
    This is all that i've gotten so far. I haven't filled in any of the other periods, because this problem is driving me insane!

    Thank you for your help.
    --Tab00

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    here is a trick that might help you - put it after your var time line:
    Code:
    console.log (typeof(time));

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What Am I supposed to do with the code? I put in in after my var time line, but when I run the code it just says string. What else am I supposed to do? I haven't very much knowledge with javascript, so i'm not really sure to do with some of the stuff.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    I'm assuming this is homework and I'm surprised that they haven't taught you this, but a string is not a number. And if you want to do things like compare one number to another then they both have to be numbers.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, this isn't Homework. My school doesn't have anything to do with programming (I'm in seventh grade). I was just trying to do this to say that I could. How would I be able to define the variable as a number?

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    seventh grade, huh? well, you're old enough to google.

    try "convert string to number javascript"

    then you might want to check out "logical operators javascript"

    and then the getHours() method (particularly how to check if it's before midnight)

    have fun, and let us know if you have any questions

  • #7
    New to the CF scene
    Join Date
    Jan 2012
    Location
    Cocoa Beach, Florida
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Possible solution

    I believe there is an error in your javascript web programming, in the following line of code:
    if (time >= 3.20 & time <= 0.00)

    The AND condition is two ampersands &&, not 1, also you should use parentheses around your evaluation statements, the following is a correct example:

    if ((time >= 3.20) && (time <= 0.00))

    The double ampersand was inherited from C and C++ programming, which uses the same symbols for logic operators, when javascript was first created in 1990s along with HTML they made alot of its operators like C/C++.

    More current web programming languages like ASP.NET and ColdFusion decided to use the actual words AND OR for thier operators, but for some reason javascript still uses the && or ||, even though it is approaching 20 years of ago.

    I have been using javascript for quite a while on our website for selling Electronic Components and Semiconductors, and have always wondered when javascript will do away with those operators.
    Last edited by WA; 01-20-2012 at 12:55 PM.

  • #8
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could definitely simplify this code instead of making 7 different variables, just using a loop or even just a recursion function.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by MichaelGWorkman View Post
    I believe there is an error in your javascript web programming, in the following line of code:
    if (time >= 3.20 & time <= 0.00)

    The AND condition is two ampersands &&, not 1,
    Incorrect. Both & and && are "AND" operators.

    & has a dual usage. It can be used to get the bitwise AND of two integers. But when used between a pair of logical values (true/false values) it performs as a logical AND.

    The only difference is the && is a "shortcut" operator where as & is not. You can look that up or ask if you don't know what that means.

    also you should use parentheses around your evaluation statements, the following is a correct example:

    if ((time >= 3.20) && (time <= 0.00))
    Why? It's certainly not a bad idea to do that. And there are *some* logical operations which might get incorrect results without added parentheses like that. But not in this case.

    ****************

    What *IS* true is that the condition here makes no sense.

    There is NO POSSIBLE VALUE for the variable time that will make the condition (time >= 3.20 & time <= 0.00) (with & or &&) be true!

    Think about it: For the first comparison to be true, time must be 3.20 or greater. For the second condition to be true, time must be zero or less. There is no possible number that can match both those conditions. So it would make just as much sense to simply code if ( false ) ...

    ********************************************
    More current web programming languages like ASP.NET and ColdFusion decided to use the actual words AND OR for thier operators, but for some reason javascript still uses the && or ||, even though it is approaching 20 years of ago.
    Also not true. Only VB.NET, of the .NET languages, uses AND and OR.

    Further, in VB.NET (and in ColdFusion and in VBScript and...) the keyword AND is equivalent to JavaScript's & operator, *NOT* the && operator. (OR is of course equivalent to the | operator, not the || operator.)

    VB.NET added the keywords ANDALSO (equivalent of &&) and ORELSE (equivalent of ||) to overcome the shortcomings of the inflexible AND and OR operators.

    And as far as && and || being obsolete: Yes, C and C++ use them. But so do C# and Java and PHP and most shell scripting languages.

    If anything is obsolete, it is the keywords AND and OR which were first introduced into BASIC in 1964. C was not officially introduced to the world until 1973, so it is MORE MODERN than BASIC.

    I have been using javascript for quite a while on our website for selling Electronic Components and Semiconductors, and have always wondered when javascript will do away with those operators.
    Probably about the same time that all the other myriad languages that use them also stop using them. In other words, when ice pervades the nether regions.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by TehMadHatter View Post
    You could definitely simplify this code instead of making 7 different variables, just using a loop or even just a recursion function.
    Loop, yes.

    Recursion? Only if you are inherently masochistic.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Oh, what the heck.

    Let's look at how and why "shortcut operators" have a huge advantage.

    Consider this piece of VBScript code (which forms the basis for ColdFusion code, too, as I understand it):
    Code:
    If Not RS.EOF AND RS("name") <> "Joe" Then ...
    That code is guaranteed to FAIL!

    Why? Because if the recordset *IS* at EOF, then the first condition of the AND is false, yes?

    BUT VBScript (and VB and...) will NEVERTHELESS attempt to test the second condition.

    But because the recordset is at EOF, the second condition creates a "no such record" error!

    Now compare that to this code, written in JScript (MS's dialect of JavaScript and also used for ASP coding):

    Code:
    if ( ! RS.EOF && RS("name") != "Joe ) ...
    This uses the SHORTCUT AND operator.

    And that means that if the first condition is false, JavaScript DOES NOT EVEN ATTEMPT to evaluate the second condition.

    So it sees that ! RS.EOF is false and stops right there. No error from the second condition!

    A *huge* advantage over the ancient AND operator of VBScript.

    And it's why VB.NET introduced the ANDALSO operator:
    Code:
    If Not RS.EOF ANDALSO RS("name") <> "Joe" Then ...
    with the ANDALSO operator, VB.NET works the same as JavaScript. When the NOT RS.EOF proves to be false, VB.NET stops right there and doesn't attempt to evaluate the second condition.

    The only solution in VBScript/ColdFusion is to use a pair of nested IF's:
    Code:
    If Not RS.EOF Then
        If RS("name") <> "Joe" Then
            ...
        End IF
    End If
    But that can fail if you wanted to do something in the ELSE case.

    So you have to resort to stuff like this:
    Code:
    Flag = Not RS.EOF
    If Flag Then Flag = ( RS("name") <> "Joe" )
    If Flag Then 
        ...
    Else
       ...
    End If
    Whereas with JS/C/C++/C#/Java/PHP code you can just do
    Code:
    if ( ! RS.EOF && RS("name") != "Joe ) 
    {
        ...
    } else {
       ...
    }
    So now tell me how VBScript and CF are superior in their use of AND and OR?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Tab00: Show us the start/end times of all 7 periods and we'll show you a shortcut way to do what you are apparently trying to do. (I think you are trying to tell the person what class they should be in at the current time, yes?)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,458
    Thanks
    0
    Thanked 632 Times in 622 Posts
    If each starts when the previous one ends then you could simply set up the times in an array and use a single if statement that uses the period number to determine the offset into the array.

    If there are gaps between the times then using two arrays would probably be the simplest to code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Tab00: Show us the start/end times of all 7 periods and we'll show you a shortcut way to do what you are apparently trying to do. (I think you are trying to tell the person what class they should be in at the current time, yes?)
    Here's the link to the bell schedule: http://www.coppellisd.com/1519208711...ule2010_11.pdf

    1st Period starts at 7:40 and goes until 8:37
    2nd Period Starts at 8:41 and goes until 9:40
    3rd Period starts at 9:44 and goes until 10:41
    4th Period starts at 10:45 and goes until 12:17
    5th Period starts at 12:21 and goes until 1:18
    6th Period starts at 1:22 and goes until 2:19
    7th Period starts at 2:23 and goes until 3:20


    Quote Originally Posted by felgall View Post
    If each starts when the previous one ends then you could simply set up the times in an array and use a single if statement that uses the period number to determine the offset into the array.

    If there are gaps between the times then using two arrays would probably be the simplest to code.
    That does sound a lot simpler, but I dont know how to use arrays yet.

    Thanks for everyone's help so far! I never expected to get so much help from the community.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Code:
    <script type="text/javascript">
    var starts = [ 0.00, 7.40, 8.41, 9.44, 10.45, 12.21, 1.22, 2.33, 3.24 ];
    var now = new Date();
    var mmss = now.getHour() + now.getMinute() / 10; 
    
    var classes = [ ];
    for ( var period = 1; s < starts.length; ++s )
    {
        classes[period] = prompt("Enter the name of your class for period " + period, "");
    }
    
    if ( mmss < 7.40 ) 
    {
        document.write( "school hasn't started yet" );
    } else if ( mmss > 3.20 ) {
        document.write( "school is out" );
    } else {
        for ( period = 1; period < starts.length; ++period )
        {
            var curstart = starts[period];
            var nextstart = starts[period+1];
            if ( mmss >= curstart && mmss <= (nextstart - 4) )
            {
                 document.write( "you should be in class " + classes[period] );
                 break;
            } else if ( mmss > (nextstart - 4) && mmss < nextstart ) {
                 document.write( "you are between classes " + classes[period] + " and " + classes[period+1];
                 break;
           }
        } // end for
    } // end else
    Off the top of my head, untested!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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