Home > Net >  How do I add together two arrays and get the result back as one number? Ruby
How do I add together two arrays and get the result back as one number? Ruby

Time:01-28

I need to add together two arrays of numbers and print the result as a total number.

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr1   arr2

Just gives me = 1 2 3 4 5 6, but I want 21.

CodePudding user response:

you can use the sum method

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]

(arr1   arr2).sum

CodePudding user response:

This is another way

arr1 = [1, 2, 3]
arr2 = [4, 5, 6]

p (arr1   arr2).reduce(: )

Output

21
  •  Tags:  
  • ruby
  • Related