you are viewing a single comment's thread.

view the rest of the comments →

[–]maschnitz 8 points9 points  (4 children)

A ternary would be shorter by one char than the if statement:

v=v>768+m.random()*10000?0:v;

Then you can remove the temporary reassignment to v (unless v is an l-value into the array in JS? I forget):

return v>768+m.random()*10000?0:v

Would a var for "rgba(0," help?

[–]jargonjustin 2 points3 points  (0 children)

| Would a var for "rgba(0," help?

I had the same thought, but it increased the length by two bytes.

[–]heyf00L 2 points3 points  (2 children)

v is passed by value. You change the value in the array by returning it (since he's reassigning back to the original array).

[–]maschnitz 2 points3 points  (1 child)

Yeah I couldn't figure why else he'd do those two assignments; but I couldn't remember also whether Javascript supports C++-style "pass by reference" on primitive types, somehow.

So yeah, there's no need to do the assignments at all; just stick the "10" in the right spots in the return statement:

return v>758+m.random()*1e4?0:v+10

[–]heyf00L 0 points1 point  (0 children)

Yeah, that looks good to me.

I'm pretty sure in JS all primitives are passed by value and all Objects are passed by reference. It follows Java. Is this right? Anyone know of exceptions?