By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

JS Operator precedence?

P: n/a
I have a script given to me by a co-worker to convert into VB; I can get
the same results but I cannot fully understand the logic here... can
someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 + Math.floor(y
/ 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????
Nov 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
newbie said the following on 11/1/2007 8:35 PM:
I have a script given to me by a co-worker to convert into VB; I can get
the same results but I cannot fully understand the logic here... can
someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 + Math.floor(y
/ 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????
You are missing the implication of the ternary operator in the original
statement.

if (m<3){
d = d + (y--)
}else{
d =
d+(y-2,Math.floor(23*m/9)+d+4+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400))%
4;)
}

The spaces were removed to try to prevent wrapping. Maybe it written as
an if/else might make it easier to see what the original code was doing.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 2 '07 #2

P: n/a
newbie wrote:
I have a script given to me by a co-worker to convert into VB; I can get
the same results but I cannot fully understand the logic here... can
someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 + Math.floor(y
/ 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????

You really have several *separate* expressions here, which I think is
what's confusing you.

"(expression1) ? (expression2) : (expression3)" is a language construct,
so it has the highest precedence here in terms of "operators" (which
it's not, really).

First, m < 3 gets evaluated. If it's true, y-- gets evaluated, and
assigned to a temporary register (call it "temp"). If not, that other
giant expression gets evaluated and assigned to temp. Then, d is
incremented by the value in temp.

Language constructs come first - other than that, operator precedence is
pretty much what you would expect. Just remember that separate
expressions get evaluated first.

Also, I hope you never use "y" again after this line, because in one
branch you affect it and in the other you don't.

Jeremy
Nov 2 '07 #3

P: n/a
In comp.lang.javascript message <472a70b7$0$17189$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Fri, 2 Nov 2007 09:35:05, newbie
<ID***************@NoSpam.composted:
>I have a script given to me by a co-worker to convert into VB; I can
get the same results but I cannot fully understand the logic here...
can someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 +
Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

That looks very much like something near the end of <URL:http://www.mer
lyn.demon.co.uk/zeller-c.htm- "rh"'s version of Mike Keith's Day-of-
Week Congruence. It would be faster if Math.floor(X) were replaced by
((X)|0). But the mod-4 is unexpected; mod-7 might well work better.

The correct translation to VBS is something like :
WeekDay(DateSerial(Y, M, D))
though the output value will need adjustment for exact agreement. And
in Javascript :
new Date(Y, M-1, D).getDay()
(but use UTC methods for speed).

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Nov 2 '07 #4

P: n/a
Jeremy wrote:
newbie wrote:
>I have a script given to me by a co-worker to convert into VB; I can
get the same results but I cannot fully understand the logic here...
can someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 +
Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;

I am happy with the major part of the maths, with the mod4, the "then"
and "else" expressions so its just the "if" part I need to "explained"...

(d += m < 3 ? THEN : ELSE, MATH_EXPRESSION) mod4;

Does the "if" part say:

d = d + m;
if(d < 3, then, else)

How does the operator precedence work here? Isn't greater-than,
less-than a higher precedence than += ? In which case:

d = d + (m < 3);
if(d, then, else)

???? or am I missing something ????


You really have several *separate* expressions here, which I think is
what's confusing you.

"(expression1) ? (expression2) : (expression3)" is a language construct,
so it has the highest precedence here in terms of "operators" (which
it's not, really).

First, m < 3 gets evaluated. If it's true, y-- gets evaluated, and
assigned to a temporary register (call it "temp"). If not, that other
giant expression gets evaluated and assigned to temp. Then, d is
incremented by the value in temp.

Language constructs come first - other than that, operator precedence is
pretty much what you would expect. Just remember that separate
expressions get evaluated first.

Also, I hope you never use "y" again after this line, because in one
branch you affect it and in the other you don't.

Jeremy
All,

Thanks for the responses...

I have re-figured the code to be "simpler" in my view to see what is
happening:

if (m<3){
d = d + (y--) //affects the value of y
}else{
d = d + (y-2) //does not change y (???)
}

var x =
(Math.floor(23*m/9)+d+4+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400))
% 4;

return x;
I don't know why one part of the function changes the value of y and the
other doesn't. As I said earlier, this is for a co-worker and she's
happy with the result. We don't need to go into how the thing works,
she's just happy she has it working, and I learned a little more about
'operator' precedence in JS.

Thanks again.
Nov 3 '07 #5

P: n/a
Dr J R Stockton wrote:
In comp.lang.javascript message <472a70b7$0$17189$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Fri, 2 Nov 2007 09:35:05, newbie
<ID***************@NoSpam.composted:
>I have a script given to me by a co-worker to convert into VB; I can
get the same results but I cannot fully understand the logic here...
can someone please help me write this in a more "understandable" way:

(d += m < 3 ? y-- : y - 2, Math.floor(23 * m / 9) + d + 4 +
Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400)) % 4;


That looks very much like something near the end of <URL:http://www.mer
lyn.demon.co.uk/zeller-c.htm- "rh"'s version of Mike Keith's Day-of-
Week Congruence. It would be faster if Math.floor(X) were replaced by
((X)|0). But the mod-4 is unexpected; mod-7 might well work better.

The correct translation to VBS is something like :
WeekDay(DateSerial(Y, M, D))
though the output value will need adjustment for exact agreement. And
in Javascript :
new Date(Y, M-1, D).getDay()
(but use UTC methods for speed).

Thanks for the response Dr Stockton. Yes the use of the pipe is faster,
so I am now using:

if (m<3){
d = d + (y--) //affects the value of y
}else{
d = d + (y-2) //does not change y (???)
}

var x = ( ((23*m/9)|0) + d + 4 + ((y/4)|0) - ((y/100)|0) + ((y/400)|0) )
% 4;

return x;
The mod4 is part of some cycle thing for scheduling. Teams work in
cycles with so-many teams working for so many days, she needs to find
the start of each four day cycle period for the team rotations, (or
something like that). I don't work shifts!

I dont know where this code comes from, I suggested using the standard
Julian Day mod4, but for some reason that wasnt sufficient? She's happy
now as she has something that works.

Thanks for your time.

Nov 3 '07 #6

P: n/a
In comp.lang.javascript message <472bcf55$0$17167$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Sat, 3 Nov 2007 10:31:05, newbie
<ID***************@NoSpam.composted:
>Dr J R Stockton wrote:
> That looks very much like something near the end of
<URL:http://www.mer
lyn.demon.co.uk/zeller-c.htm- "rh"'s version of Mike Keith's Day-of-
Week Congruence.
>The mod4 is part of some cycle thing for scheduling. Teams work in
cycles with so-many teams working for so many days, she needs to find
the start of each four day cycle period for the team rotations, (or
something like that). I don't work shifts!

I dont know where this code comes from,
I indicated it as quoted above. Wikipedia search for "Mike Keith" leads
easily to <http://users.aol.com/s6sj7gt/mikecal.htm>, which my page also
links to. My page notes that LF & MK have since shortened the
expression.
I suggested using the standard Julian Day mod4,
The standard Julian Date changes at Noon UTC, which is unlikely to be
wanted.
but for some reason that wasnt sufficient? She's happy now as she has
something that works.
The formula was developed for day-of-week, with % 7 at the end. The
month term ignores the first 28 days of the months that have passed,
since that is a multiple of 7. It also happens to be a multiple of 4,
and the modified formula works for you. But ISTM that if there is a
change to, say, 3 or 5 shifts, then greater change will be needed.
The following method is faster in IE6, and easier to understand and to
modify.

Date.UTC(y, m-1, d) / 864e5 % 4

VBS? DateSerial(y, m, d) % 4

In each case one should be able to adjust the phase by adding a constant
to d.

=======

Firefox 2.0.0.9 retains the Date.UTC(y, m, 0) error.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Nov 4 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.