Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

jslint_com · This group has moved to Google Plus.

The Yahoo! Groups Product Blog

Check it out!

Group Information

  • Members: 583
  • Category: JavaScript
  • Founded: Mar 7, 2008
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Messages

Advanced
Messages Help
Just got confused with the `?` operator   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#3118 From: "Felix E. Klee" <felix.klee@...>
Date: Tue Feb 5, 2013 5:14 pm
Subject: Just got confused with the `?` operator
feklee
Send Email Send Email
 
Well, today I'm not in good shape, and I expected the following code to
output just one zero. The code passes JSLint.

/*jslint devel: true */

(function () {
'use strict';

var isOneWayFlight = true, i;

for (i = 0; i < isOneWayFlight ? 1 : 2; i += 1) {
console.log(i);
}
}());

Instead, the runtime enters an infinite loop. It outputs: 0, 1, 2, ...

Correct version of the for statement, doing what I want:

for (i = 0; i < (isOneWayFlight ? 1 : 2); i += 1) {
console.log(i);
}

That's just FYI.



#3119 From: Keradus <keradus@...>
Date: Tue Feb 5, 2013 5:30 pm
Subject: Re: [jslint] Just got confused with the `?` operator
keradmaster
Send Email Send Email
 
It's a matter of priority.
First "<" will be executed, then "?".

So
i < isOneWayFlight ? 1 : 2
is the same as
(i < isOneWayFlight) ? 1 : 2

regarding to initial values:
true ? 1 : 2 => 1


And then your loop looks like this:
for (i = 0; 1; i += 1) {...}
That's why it's continues indefinitely




--
Keradus



#3120 From: "Felix E. Klee" <felix.klee@...>
Date: Tue Feb 5, 2013 5:40 pm
Subject: Re: [jslint] Just got confused with the `?` operator
feklee
Send Email Send Email
 
On Tue, Feb 5, 2013 at 6:30 PM, Keradus <keradus@...> wrote:
> It's a matter of priority.

I know. Still the error slipped in, out of sloppiness, like many errors
that JSLint is designed to capture.



#3121 From: "george_weilenmann" <abyssoft@...>
Date: Tue Feb 5, 2013 10:17 pm
Subject: Re: [jslint] Just got confused with the `?` operator
george_weile...
Send Email Send Email
 
This is more of an error on the part of the knowledge of the developer
then on the part of JSLint. The code pattern is perfectly valid and not
confusing to someone who has a good grasp on operator precedence.
even if JSLint was to require () around the ternary operator and
results.
Such that your line
for (i = 0; i < isOneWayFlight ? 1 : 2; i += 1) {
would have thrown an error for missing () around ternary.You still could
have done
for (i = 0; (i < isOneWayFlight ? 1 : 2); i += 1) {
and the same error would have occurred in your logic.
See http://www.javascriptkit.com/jsref/precedence_operators.shtml for a
nice table on precedence of operators.


--- In jslint_com@yahoogroups.com, "Felix E. Klee" wrote:
>
> On Tue, Feb 5, 2013 at 6:30 PM, Keradus wrote:
> > It's a matter of priority.
>
> I know. Still the error slipped in, out of sloppiness, like many
errors
> that JSLint is designed to capture.
>



[Non-text portions of this message have been removed]




#3122 From: "Felix E. Klee" <felix.klee@...>
Date: Tue Feb 5, 2013 11:10 pm
Subject: Re: [jslint] Just got confused with the `?` operator
feklee
Send Email Send Email
 
On Tue, Feb 5, 2013 at 11:17 PM, george_weilenmann <abyssoft@...>
wrote:
> This is more of an error on the part of the knowledge of the developer

Exactly not. I'm a C programmer for more than 20 years, and I'm well
versed with its operator precedence, which is similar to that of
JavaScript. I frequently use the `?` operator, and without any issues.

I just wanted to point out what happened to me today. That's all.



#3123 From: "douglascrockford" <douglas@...>
Date: Wed Feb 6, 2013 3:47 am
Subject: Re: Just got confused with the `?` operator
douglascrock...
Send Email Send Email
 
--- In jslint_com@yahoogroups.com, "Felix E. Klee"  wrote:
> /*jslint devel: true */
>
> (function () {
> 'use strict';
>
> var isOneWayFlight = true, i;
>
> for (i = 0; i < isOneWayFlight ? 1 : 2; i += 1) {
> console.log(i);
> }
> }());
>
> Instead, the runtime enters an infinite loop. It outputs: 0, 1, 2, ...
>
> Correct version of the for statement, doing what I want:
>
> for (i = 0; i < (isOneWayFlight ? 1 : 2); i += 1) {
> console.log(i);
> }


Thanks. Please try it now.




#3125 From: "Felix E. Klee" <felix.klee@...>
Date: Thu Feb 7, 2013 9:49 am
Subject: Re: [jslint] Re: Just got confused with the `?` operator
feklee
Send Email Send Email
 
On Wed, Feb 6, 2013 at 3:47 AM, douglascrockford <douglas@...> wrote:
> Please try it now.

So quick - thanks! :-)



#3124 From: "george_weilenmann" <abyssoft@...>
Date: Wed Feb 6, 2013 4:12 am
Subject: Re: Just got confused with the `?` operator
george_weile...
Send Email Send Email
 
Nice compromise. Works great and indeed improves readability.

--- In jslint_com@yahoogroups.com, "Felix E. Klee" wrote:
>
> Well, today I'm not in good shape, and I expected the following code to
> output just one zero. The code passes JSLint.
>
> /*jslint devel: true */
>
> (function () {
> 'use strict';
>
> var isOneWayFlight = true, i;
>
> for (i = 0; i < isOneWayFlight ? 1 : 2; i += 1) {
> console.log(i);
> }
> }());
>
> Instead, the runtime enters an infinite loop. It outputs: 0, 1, 2, ...
>
> Correct version of the for statement, doing what I want:
>
> for (i = 0; i < (isOneWayFlight ? 1 : 2); i += 1) {
> console.log(i);
> }
>
> That's just FYI.
>





 
Add to My Yahoo!      XML What's This?

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help