On Fri, Oct 9, 2009 at 5:56 PM, Nick W. [email protected] wrote:
don’t know how to add up the scores that are entered. Any help would be
Posted via http://www.ruby-forum.com/.
I haven’t looked at your code so probably shouldn’t be answering this
To add up the values in an array you can do something like this:
total = 0
score_array.each do |score|
total += score
The each method will step through your score_array one entry at a time
put that value into score. The bit between the score_array.each do
and end just adds the score (value of each array position) to the total.
The last line prints the total.
NOTE if total doesn’t exist before the each method is invoked it will
disappear when each finishes, that can be a bit surprising when you are
starting out. This happens because Ruby automatically generates a
the first time it sees it, so it would get created inside the each call.
However each is using a block here which creates it’s own scope so as
as it finishes total goes out of scope and is no longer available.
“Hey brother Christian with your high and mighty errand, Your actions
so loud, I canâ€™t hear a word youâ€™re saying.”
-Greg Graffin (Bad Religion)