ruby - Loop through an array while summing the output -


i trying use for loop , sum of outputs. know basic question, not sure if should trying solve within loop or using array.

for example:

for in 1..100 if foo / 2 == 0 #find sum of outputs end 

end

try this:

(2..100).step(2).inject(:+) #=> 2550 

or:

2.step(100,2).inject(:+) #=> 2550 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -