# 5 - can't get the n.times syntax right

#1

I can't wrap my head around how to replace the "for" loop with an n.times can anyone give a little guidance? Thanks in advance!

### ORIGINAL

prime = Prime.new
for num in (1..n)
prime_array.push(prime.next)
end
prime_array
end

### MINE

prime = Prime.new
n.times {prime_array.push(prime.next)}
prime_array
end

#2

I don't understand the n.times part. Why wouldn't it just be 10.times ??? Can someone explain? Thanks in advance.

#3

Because the method we're attempting to define is intended to return n number of primes... 10 is just an example. If we hard-code 10 in, the method won't work for anything other than 10.

#4

... that said, my code was the same as mattlynch37's, and I don't understand why it isn't working either.

#5

Remove your prime-array on the 3rd line and it should work

#6

profister is correct. Take out the prime_array because there is no need to return a value since it is implicit

#7

That's not it. To be honest I'm totally confused because I tried that and many other things for days to no avail. Finally just now I got the green light and I'm not sure why but here's my code for those still stuck:

\$VERBOSE = nil # We'll explain this at the end of the lesson.
require 'prime' # This is a module. We'll cover these soon!

def first_n_primes(n)

return "n must be an integer." unless n.is_a? Integer

return "n must be greater than 0." if n <= 0

prime_array ||= []

prime = Prime.new
n.times {prime_array.push(prime.next)}
prime_array
end

first_n_primes(10)

#8

This gets me through:
prime = Prime.new
n.times {prime_array.push(prime.next)}
prime_array

While this gets an infinite loop:
prime = Prime.new
n.times
{prime_array.push(prime.next)}
prime_array

The difference is the new line after n.times. It's confusing because it appears this way in the hint. I didn't realize the code between the {} had to be on the same line.

#9

Thanks scriptpro90681. I couldn't get it through without your hint.