Hi,
I’ve created the attached program but can’t get my random number to pass
into my array in place of another item in the array, can anyone help?
Many Thanks
Richard
Hi,
I’ve created the attached program but can’t get my random number to pass
into my array in place of another item in the array, can anyone help?
Many Thanks
Richard
The problem is in the “run” method: every time you call the “string”
method in there, you are basically resetting the @string instance
variable to its starting value.
Maybe try experimenting with using the @string variable directly in the
run method (instead of via the “string” method as you do currently) and
you will have better results!
Richard J. wrote in post #1128219:
Hi,
I’ve created the attached program but can’t get my random number to pass
into my array in place of another item in the array, can anyone help?Many Thanks
Richard
A couple of reasons, but here’s a big one:
string[0] = @new_number
… calls the `string’ function which, in your code, creates and returns
a new array, whose elements are [25,50,75,100]. Then it overrides the
0th element (the 25) with whatever value @new_number has, which in this
case is a random number up to 15.
puts string
… calls the string' function again, which creates and returns a new array whose elements are [25,50,75,100]. Then it
puts’es that new
array.
The other big problem is: in your if-elsif tests, you’re comparing if number == x' instead of
if answer = x’.
Here’s a pattern I propose as a general rule for OOP:
number' and
string’ functions should not include anWithout doing anything clever, apart from fixing the condition in the
`if’ statements, and applying my three guidelines above, I get the
attached script. There are other things you could do to make it more
“rubyish”, but this is a start.
On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:
–
Posted via http://www.ruby-forum.com/.
two major problems others have said you are resetting @string every
time you call the string method. The other problem is your if-elsif
struct you input into the answer local variable, but youre checking
number instead.
On Nov 23, 2013 6:52 AM, “Tamara T.” [email protected]
wrote:
two major problems others have said you are resetting @string every
time you call the string method. The other problem is your if-elsif
struct
you input into the answer local variable, but youre checking number
instead.
Others have said that, too. (Modulo a typo with a missing equals sign.)
That’s okay, though, I did use a lot of words; I shouldn’t expect people
to
look at all of them…
Sent from my phone, so excuse the typos.
Try this, you were close. I would try test driving you application to
help you make sure your program is doing what you want it to.
You can do that on small projects like this.
Todd Pickell
Software Developer
On November 22, 2013 at 2:52:34 PM, Tamara T.
([email protected]) wrote:
On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:
–
Posted via http://www.ruby-forum.com/.
two major problems — others have said you are resetting @string every
time you call the string method. The other problem is your if-elsif
struct — you input into the answer local variable, but you’re checking
number instead.
Todd Pickell
Software Developer
On November 22, 2013 at 3:11:41 PM, Todd Pickell ([email protected])
wrote:
Try this, you were close. I would try test driving you application to
help you make sure your program is doing what you want it to.
You can do that on small projects like this.
Todd Pickell
Software Developer
On November 22, 2013 at 2:52:34 PM, Tamara T.
([email protected]) wrote:
On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:
–
Posted via http://www.ruby-forum.com/.
two major problems — others have said you are resetting @string every
time you call the string method. The other problem is your if-elsif
struct — you input into the answer local variable, but you’re checking
number instead.
Todd Pickell wrote in post #1128342:
Here is a slightly better version.
Actually, your second one has a zero-index issue, because you can input
numbers 1,2,3,4, but the array indices are 0,1,2,3. Did you try driving
your application to make sure your program is doing what you want it to?
Todd Pickell
Software Developer
On November 22, 2013 at 3:45:47 PM, Matthew K.
([email protected]) wrote:
Todd Pickell wrote in post #1128342:
Here is a slightly better version.
Actually, your second one has a zero-index issue, because you can input
numbers 1,2,3,4, but the array indices are 0,1,2,3. Did you try driving
your application to make sure your program is doing what you want it to?
–
Posted via http://www.ruby-forum.com/.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs