473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JS Operator precedence?

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
6 1836
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3199
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
20
8118
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
4
4671
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
17
3464
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
21
3000
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
56
4695
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
9
3729
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
5
2889
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
8
7820
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.