ruby: sum corresponding members of two or more arrays

前端 未结 9 804
孤独总比滥情好
孤独总比滥情好 2020-12-13 09:01

I\'ve got two (or more) arrays with 12 integers in each (corresponding to values for each month). All I want is to add them together so that I\'ve got a single array with su

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 09:39

    This might not be the best answer but it works.

    array_one = [1,2,3]
    array_two = [4,5,6]
    x = 0
    array_three = []
    while x < array_one.length
      array_three[x] = array_one[x] + array_two[x]
      x += 1
    end
    
    =>[5,7,9]
    

    This might be more lines of code than other answers, but it is an answer nonetheless

提交回复
热议问题