The prompt is this:
“Q2: Write a method, sum which takes an array of numbers and returns the
sum of the numbers.”
Thus far, I have:
"def sum(nums)
total = 0
i = 0
while i < nums.count
total += nums[i]
i += 1
end
total
end
puts ‘Enter a series of numbers. Enter ‘Done’ when you are done.’
input = gets.chomp.to_i
while true
puts ‘Now another:’
input = gets.chomp
nums = []
nums.push input.to_i
if input.to_s == ‘Done’
nums.pop input.to_i
break
end
end
puts nums
sum(nums)"
I have been testing the program by inputting 1, then 2, then 3, and
finally Done. This has been the output:
“Enter a series of numbers. Enter ‘Done’ when you are done.
1
Now another:
2
Now another:
3
Now another:
Done
0”
I’m not sure if the inputs aren’t being added to the array, or if
inputting “Done” erases all objects in the array, or what. I’ll
appreciate any help in solving this :]