So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.
You're going to have to count them. a positive number is 0 or greater, use that as your check. You could do an if statement and count the positives and elsif to count negatives, and have a nice routine to use at anytime.
Wayne
···
________________________________
From: Johanna Bettina <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Sent: Tuesday, December 17, 2013 2:25 PM
Subject: Count numbers - please help
Hello, can you please help me :)?
I have an array like this:
a=[1,-8,4,6]
there a positive and negative numbers in.
but now i only want to count the positive.
So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.
On 2013-Dec-17, at 15:25 , Johanna Bettina <lists@ruby-forum.com> wrote:
Hello, can you please help me :)?
I have an array like this:
a=[1,-8,4,6]
there a positive and negative numbers in.
but now i only want to count the positive.
So now I have to make a function which returns the number of positive
numbers (in this time 3). I have tried, but I also managed to count all
numbers. but I need only to count the positive numbers. And I should do
it with a loop.
At the first iteration, count is equal to the argument of inject ( = 0 )
At each iteration count is equal to the last value returned.
The block returns the same value (count) or the incremented (count+1)
if the element is >=0.
Look at
But, the Rob Biedenharn way is the "right" way to go.
a.count {|e| e > 0}
Best regards,
Abinoam Jr.
···
On Wed, Dec 18, 2013 at 1:12 PM, Johanna Bettina <lists@ruby-forum.com> wrote:
Thank you so much :). That helped me! Now I understand :).