I should have posted this here in the first place. My Question follows
with the Answer from Christopher.
Geoff
On Wed, Apr 27, 2011 at 9:19 AM, Geoff Longman <
glon...@gmail.com>
wrote:
Dude,
Was checking it out a little more last night and I can't figure out
how to write the following in Coffescript:
(contrived example)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9. 10, 11, 12]
var result = false
for (i=0, len = a.length, i < len; i++) {
if (a[i] == 3) {
result = true;
break; // stop looping
}
}
I can't seem to figure out the "stop looping" case in CS syntax.
If that array had 10,000 entries I dont want to loop 10,000 times if
the 3rd entry matched.
CS comprehensions appear to want to iterate over all entries and
collect the results.
Any suggestions???
--
Geoff
Chris Says
=========
a = [1..12]
for x in a
if x == 3
result = true
break
if result?
console.log "woohoo"
Note: Since coffeescript defines all your variables at the top you can
pull this off. Also this is a truthiness test, so in the if result?
case where we couldn't find x == 3, result would be undefined/null
which technically counts as false.
--
Christopher Saunders
http://christophersaunders.ca/