Translating from Javascript to Ruby -


i'm trying translate below js code ruby. i'm not looking solution, let me know concepts should take ruby translate? need place start. code here:

var sum = 0; for(var x = 0; x < 1000; x++) { if (x%3 === 0 || x%5 === 0) {     sum += x;   } }  console.log(sum); 

sum = 0 x in 0..1000   if x % 3 == 0 || x % 5 == 0     sum += x   end end puts sum 

but i'd do

puts (0..1000).inject {|acc, x| if x % 3 == 0 || x % 5 == 0; acc += x; end; acc} 

check out ruby syntax http://learnxinyminutes.com/docs/ruby/ , see here http://ruby-doc.org/core-2.2.2/enumerable.html#method-i-inject information on inject. (it called fold in other languages.)


Comments