4 ms·
I'd write that snippet the following way: for (var i = 0, ii = list.length; i < ii; i++) { sum += list[i].amount; } Or: for (var i = 0, i
by augustl 15y ago
I'd write that snippet the following way:
for (var i = 0, ii = list.length; i < ii; i++) {
sum += list[i].amount;
}
Or:
for (var i = 0, ii = list.length; i < ii; i++)
sum += list[i].amount;
There are ways to make normal JavaScript not look ugly ;)
EDIT: fixed minor oversight
- haydenchambers 15y agoAnd where do you get item from?
- jimbokun 15y agoThe small bug in your code actually makes a good case for using the more concise syntax of coffeescript to help avoid those kinds of errors, while generating code that's just as efficient.
- augustl 15y agoI disagree. In a real programming environment I'd notice immediately after running the code.
- utkarshkukreti 15y agoWhere are you setting `item` here?
- TrevorBurnham 15y agoAnd I'd write for i in [0...list.length] by 1 sum += list[i].amount to get equivalent compiled output. I've used C and Java since high school, but I'm happy to be rid of that old `for` syntax. And I love that curly braces means only one thing in CoffeeScript: "I'm defining a new object with these key-value pairs."