Prev: nike air max 2003 ney style hot sell ....free shipping to worldwide
Next: cluttered code prevention?
From: dsmithy on 19 Oct 2009 20:53 Hi, I came across this line of code in a book. It appears to use the alternative if syntax, but it doesn't quite make sense to me: res += (i * j) % 8 ? " " : "*"; where res is a string variable, and i and j are number variables (i increments from 1 to 7 and j increments from 1 to 15). What's confusing me is that I thought the expression preceding the ? had to be a conditional (i.e., true or false) statement in order for the computer to choose between " " and "*". But (i * j) % 8 isn't a conditional. It just evaluates to a number. So how is the choice made between " " and "*"? Thank you,
From: rf on 19 Oct 2009 21:05 "dsmithy" <dsmithy(a)live.ca> wrote in message news:88ca9c69-59b3-4761-ba6b-4e5f9a4bb38a(a)31g2000vbf.googlegroups.com... > Hi, > I came across this line of code in a book. It appears to use the > alternative if syntax, but it doesn't quite make sense to me: > > res += (i * j) % 8 ? " " : "*"; This is not an if statement. > where res is a string variable, and i and j are number variables (i > increments from 1 to 7 and j increments from 1 to 15). > > What's confusing me is that I thought the expression preceding the ? > had to be a conditional (i.e., true or false) statement in order for > the computer to choose between " " and "*". But (i * j) % 8 isn't a > conditional. It just evaluates to a number. Correct. And for numbers 0 is false and anything else is true.
From: dsmithy on 19 Oct 2009 21:24 On Oct 19, 9:05 pm, "rf" <r...(a)z.invalid> wrote: > > res += (i * j) % 8 ? " " : "*"; > > This is not an if statement. Well, the "if" keyword certainly isn't present, but the author of the book I'm reading does refer to this construction as an "alternative if" syntax. > > What's confusing me is that I thought the expression preceding the ? > > had to be a conditional (i.e., true or false) statement in order for > > the computer to choose between " " and "*". But (i * j) % 8 isn't a > > conditional. It just evaluates to a number. > > Correct. And for numbers 0 is false and anything else is true. Aha! You're right. I didn't make the connection with type conversions. Thank you for your helpful reply.
From: Thomas 'PointedEars' Lahn on 19 Oct 2009 21:40 dsmithy wrote: > On Oct 19, 9:05 pm, "rf" <r...(a)z.invalid> wrote: >> > res += (i * j) % 8 ? " " : "*"; >> >> This is not an if statement. > > Well, the "if" keyword certainly isn't present, but the author of the > book I'm reading does refer to this construction as an "alternative > if" syntax. Sounds like just another bad book. Dump it. >> > What's confusing me is that I thought the expression preceding the ? >> > had to be a conditional (i.e., true or false) statement in order for >> > the computer to choose between " " and "*". But (i * j) % 8 isn't a >> > conditional. It just evaluates to a number. >> >> Correct. And for numbers 0 is false and anything else is true. > > Aha! You're right. They are not. > I didn't make the connection with type conversions. Although that is probably one of the first thing a book on ECMAScript scripting should teach. The number of people asking why "1" + 2 === "12" being a telltale sign of that. > Thank you for your helpful reply. Hmmm. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Thomas 'PointedEars' Lahn on 19 Oct 2009 21:43
Thomas 'PointedEars' Lahn wrote: > To make a long story short, Number values are converted to Boolean so that > (±)`0' and `NaN' are converted to `true', and all other values to `false'. It is the other way around, of course: (±)`0' and `NaN' are converted to _false_, and all other values to _true_. Sorry for causing confusion. PointedEars, saying goodnight -- Danny Goodman's books are out of date and teach practices that are positively harmful for cross-browser scripting. -- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004) |