Refactor the code on line 14 to use conditional assignment to set prime_array to [] instead of using an if statement


#1



Oops, try again.
It looks like you didn't correctly use the conditional assignment operator in your code. Check the Hint if you need help!
can you check line 14. prime_array ||= [] #if prime_array.nil?

https://www.codecademy.com/courses/ruby-beginner-en-Zjd2y/0/2?curriculum_id=5059f8619189a5000201fbcb#


$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)

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

  if n <= 0
    return "n must be greater than 0."
  end
  
  prime_array ||= [] #if prime_array.nil?
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end


#2

I think we can drop the ||.

    prime array = [] if prime_array.nil?

This is a protective measure to prevent redefining the array if it already exists. That would overwrite it.


#3

The exercise wants us to use the ||. " Refactor the code on line 14 to use conditional assignment to set prime_array to [] instead of using an if statement." Please help.


#4

This is a condtional assignment with if. I've finally got to reading the instructions and see what you mean. My bad.

    prime array = [] if prime_array.nil?

This is what you have, isn't it?

    prime_array ||= []

Your code looks correct, Did you call your method?

first_n_primes(10)

#5

Yes, I called the method. It still gives me the same error when I comment out the if and add || to =[].
Can you help?


#6

Will need to see your complete code sample, again, please. Thank you.


#7

Here's the complete code:

$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)

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

  if n <= 0
    return "n must be greater than 0."
  end
  
  prime_array ||= [] #if prime_array.nil?
  
  prime = Prime.new
  for num in (1..n)
    prime_array.push(prime.next)
  end
  return prime_array
end

#8

I've tested your code and it passes. Perhaps try to refresh the page and submit again. Your code should be saved.


#9

You're right it works. I got the error message again until I erased the commented out if clause. Thanks for your help.


#10

Hello sir, I am really having trouble understanding your last line..

"This is a protective measure to prevent redefining the array if it already exists. That would overwrite it."

The line in the code confuses me, both versions too.. You say redefining what do you mean? Thanks


#11

When we redefine a variable, it no longer has its earlier value(s). It is the same as defining it anew. When we are accumulating to an array, we only define it once, when we initialize, and never again. Hence, the safeguard built into the code.


#12

Thank you for your reply. The course didn't explain any of that.

prime_array ||= []

prime_array = [] if prime_array.nil?

How do these variables accomplish that? Or what exactly is it doing. For the if statement. It is is creating a new empty area, and then it is says if its empty?

And the top one. It's basically saying this array is empty and can't be set equal empty again.
Thanks again


#13

prime_array ||= [] # if prime_array.nil?
                   # comment not part of statememt

The logic is built into the operator behavior. OR-ASSIGN

In an OR expression, only one operand needs to be true for the expression to be true. No matter how many operands, the first true will short-circuit the expression and exit with a value of true.

Consider,

    a = false
    b = true
    c = false
    d = true

if a || b || c || d
    # do something

In the above, only operand a and b are evaluated. c and d are never seen.

true ||= []

will evaluate to true.

false ||= []

will evaluate to []

So when prime_array is non-existent (nil?),

prime_array

will evaluate to false, and the new array is defined.