3/7 Omit Needless Words, what is wrong with my code?

original code:

unless n.is_a? Integer
return “n must be an integer.”
end

^^^The instructions are to refractor the code to use a single-line if and
unless

so here is my code that i changed it to:

puts “n must be an integer.” unless integer
puts "n must be greater than 0."

the output reads: “n must be greater than zero”

but then i get an Error pop up that says “The program took too long to finish. Check your code for infinite loops and try again.”

WHAT IS GOING ON HERE? HELP!

Should you be returning something?

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

return "n must be greater than 0." if n <= 0
$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 = [] if prime_array.nil?
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

first_n_primes('x')
2 Likes

A post was split to a new topic: When do we need to use return