...

View Full Version : question regarding compound statements



diverdee
05-29-2007, 02:00 AM
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:

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?:

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


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.

liorean
05-29-2007, 11:40 AM
Today = new date();
DayNumber = Today.getDay();
Hour = Today.getHours();
Minute = Today.getMinutes();
date --> Date



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.


[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.

}
else
{Again, prevailing style in JavaScript would put that closing curly brace, else and opening curly brace on one and the the same line.



}
};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.


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
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
a && b || c && dis the same as
(a && b) || (c && d)
and not
a && (b || c) && d

diverdee
05-29-2007, 09:37 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum