You could remove your repeated `up ? ... : ...` statements here and below if you define a `var dir = up ? -1 : 1` and use that as an intermediate.