Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-29-2007, 01:00 AM   PM User | #1
diverdee
New to the CF scene

 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
diverdee is an unknown quantity at this point
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.
diverdee is offline   Reply With Quote
Old 05-29-2007, 10:40 AM   PM User | #2
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Quote:
Originally Posted by diverdee View Post
Code:
Today = new date();
DayNumber = Today.getDay();
Hour = Today.getHours();
Minute = Today.getMinutes();
date --> Date

Quote:
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.
Quote:
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.
Quote:
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.

Quote:
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.
Quote:
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';
}
Quote:
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.
Quote:
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
__________________
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

Last edited by liorean; 05-29-2007 at 10:42 AM..
liorean is offline   Reply With Quote
Old 05-29-2007, 08:37 PM   PM User | #3
diverdee
New to the CF scene

 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
diverdee is an unknown quantity at this point
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.
diverdee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:46 AM.


Advertisement
Log in to turn off these ads.