Ruby on Rails Tuesday, August 4, 2015

Colin,

That's very helpful.

Sorry, I copy-pasted the codes I had from my word file so I missed the
"def."

Anyway,

Thank you for explaining. I have two more questions which I will ask in
another thread after I finish doing them on my own.

Do you have a personal e-mail I can reach you on? I would love to pick
your brain if you give me a chance.

Best,

DB



Colin Law wrote in post #1177221:
> On 4 August 2015 at 03:56, Dhaval Bhatt <lists@ruby-forum.com> wrote:
>> oddball_sum(numbers)
> You need a def on the front of the above
>
>> i = 0
>
> You need a variable to hold the sum, so
> result = 0
>
>> while i < numbers.length
>> if (numbers % 2 !== 0)
>
> you meant numbers[i] % 2 != 0
>
>> return numbers[i] += 1
>
> Don't return here, add this number to the total
> result += numbers[i]
>
>> i += 1
>> end
>> return result
>> end
>
> But in fact it is better to use each, not a while loop, so something
> like
>
> def oddball_sum(numbers)
> result = 0
> numbers.each { |n| result += n if n %2 != 0 }
> return result
> end
>
> In fact there are even more concise ways of coding this, but I would
> stick with something like the above for the moment in order to keep
> the code readable.
> I suggest you find some tutorials on Ruby first and work through them,
> then when you think you have the basics of Ruby sorted move on to a
> good Rails tutorial such as railstutorial.org, which will show you the
> basics of Rails.
>
> Colin

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5c16fda63b896406574130cbacf9500d%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment