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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    question regarding compound statements

    Hi, hoping someone could help answer this question for me.
    I'm a newbie with javascript, just started learning the basics this week as it will be useful at my new workplace.
    Done some minimal coding a long time ago.
    I'm wondering about compound statements in javascript, see I was always taught to keep code as short & sweet as possible.
    One of the guys where i've started working uses a lot of nested statements for example:
    Code:
    Today = new date();
    DayNumber = Today.getDay();
    Hour = Today.getHours();
    Minute = Today.getMinutes();
    
    if (DayNumber >=1 && DayNumber <=5)
    { 
    if (Hour >= 8 && Hour <= 19)
    	{
    	[var_co] = 'yes'
    	}
    	else
    	{[var_co] = 'no'
    	}
    };
    else if (DayNumber == 6)
    {
    if (Hour >= 8 && Hour <=13)
    	{
    	[var_co] = 'yes'
    	}
    	else
    	{
    	[var_co] = 'no'
    	}
    };
    else
    	{
    	[var_co] = 'no'
    	};
    Dunno, maybe i'm wrong in my assumptions, being out of the loop for years but would the following compound statement be equivalent to the first one/work in javascript?:
    Code:
    Today = new date();
    DayNumber = Today.getDay();
    Hour = Today.getHours();
    Minute = Today.getMinutes();
    
    if 	(((DayNumber >=1 && DayNumber <=5) && (Hour >=8 && Hour <=19))
    ||  	((DayNumber ==6) && (Hour >=8 && Hour <=13)))
    	{
    	[var_co] = 'yes'
    	};
    else
    	{
    	[var_co] = 'no'
    	};
    See when the particular piece of scripting needed to be done I (still only learning the basics really) pretty much straight away sketched the solution out as in the second example, i.e
    Code:
    if (((true) && (true))
    or  ((true) && (true)))
       {yes};
    else
       {'no'};
    Just wondering whether that's correct syntax in javascript.
    As I said, i'm still picking up the basics & haven't really seen much information on deeply compounded statements of this type, most of the examples are pretty much 'if && else' or 'if or' etc.
    Any help/elucidation would be much appreciated.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by diverdee View Post
    Code:
    Today = new date();
    DayNumber = Today.getDay();
    Hour = Today.getHours();
    Minute = Today.getMinutes();
    date --> Date

    Code:
    if (DayNumber >=1 && DayNumber <=5)
    {
    Prevailing style in JavaScript is to put the curly brace at the end of the same line as the if-statement.
    Code:
    	[var_co] = 'yes'
    [var_co] is not a correct left hand side of an assignment. That should probably be just var_co of that is the name of the variable.
    Code:
    	}
    	else
    	{
    Again, prevailing style in JavaScript would put that closing curly brace, else and opening curly brace on one and the the same line.

    Code:
    	}
    };
    And that colon should not be there. It's an empty statement, which means the else on the next line is a syntax error. Remove it.
    Code:
    else if (DayNumber == 6)
    {
    if (Hour >= 8 && Hour <=13)
    	{
    	[var_co] = 'yes'
    	}
    	else
    	{
    	[var_co] = 'no'
    	}
    };
    else
    	{
    	[var_co] = 'no'
    	};
    Same style corrections are appropriate here. With the normal conventions for JavaScript code, that would turn the code into
    Code:
    var
        Today = new Date(),
        DayNumber = Today.getDay(),
        Hour = Today.getHours(),
        Minute = Today.getMinutes();
    
    if (DayNumber >=1 && DayNumber <=5) { 
        if (Hour >= 8 && Hour <= 19){
            var_co = 'yes';
        } else {
            var_co = 'no';
        }
    } else if (DayNumber == 6) {
        if (Hour >= 8 && Hour <=13) {
            var_co = 'yes';
        } else {
            var_co = 'no';
        }
    } else {
        var_co = 'no';
    }
    Dunno, maybe i'm wrong in my assumptions, being out of the loop for years but would the following compound statement be equivalent to the first one/work in javascript?
    From a glance, yes.
    Just wondering whether that's correct syntax in javascript.
    As I said, i'm still picking up the basics & haven't really seen much information on deeply compounded statements of this type, most of the examples are pretty much 'if && else' or 'if or' etc.
    Any help/elucidation would be much appreciated.
    They work like this:

    && - If the left hand side is falseish (becomes false when converted to boolean) the left hand side is returned. If the left hand side is truthish (becomes true when converted to boolean) the right hand side is returned.

    || - If the left hand side is truthish the left hand side is returned. If the left hand side is falseish the right hand side is returned.


    That means it's the actual value that is returned, not a boolean.



    The && operator binds stronger than the || operator, meaning
    Code:
    a && b || c && d
    is the same as
    Code:
    (a && b) || (c && d)
    and not
    Code:
    a && (b || c) && d
    Last edited by liorean; 05-29-2007 at 10:42 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks liorean, nice to know that my assumptions were correct-ish.
    Regarding the syntax, I had issues with that myself (from some initial perusing of some reference works I was lent), but the code is all part of a kind of proprietary call-scripting application & that seems to be the standard that the applications built in script-builder defaults to & therefore the standard that the people who have been maintaining/building the scripts have gotten used to.
    I will, however, endeavor to change that if possible - as I don't want to fall into 'bad' habits.
    Regarding the square brackets surrounding the variables (e.g. [var_co] ), again that seems to be the standard that has been adopted within the particular application, I guess i'll find out more when I have the manual printed off tomorrow.
    The explanation of the '&&' & '||' expressions makes perfect sense also.
    Again thanks, you've been a great help.


  •  

    Posting Permissions

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