7. Nice Work -> is it missing a couple of return statements?


#1


  1. Nice Work https://www.codecademy.com/en/courses/ruby-beginner-en-Zjd2y/0/7

    I'm getting "comparison of String with 0 failed" instead of the expected statement that "n must be an integer"

Here's the code I'm working with - notice the last line where I replaced 10 with "a"

require 'prime'

def first_n_primes(n)
  # Check for correct input!
  "n must be an integer" unless n.is_a? Integer
  "n must be greater than 0" if n <= 0

  # The Ruby 1.9 Prime class makes the array automatically!
  prime = Prime.instance
  prime.first n
end

first_n_primes("a")

I think the first two statements after the def should have a return like this:

  "n must be an integer" unless n.is_a? Integer
  "n must be greater than 0" if n <= 0

however it seems that the code as it is was supposed to show us the "streamlined" version of the code? But it's not really working though. Maybe I'm missing something?


#2

No insult intended, you are, Ruby permits implicit returns. Something to read up on that may add to your tool kit.


#3

Hm. But I thought implicit returns only work if it's the last line in the method definition or scope?

I've attached a screenshot of the editor window that I see immediately after clicking the challenge link.

The only thing I've done is to change the 10 into "a" in an effort to exercise the # Check for correct input! above.

However, this works as expected:

def first_n_primes(n)
  # Check for correct input!
  return "n must be an integer" unless n.is_a? Integer
  return "n must be greater than 0" if n <= 0

  # The Ruby 1.9 Prime class makes the array automatically!
  prime = Prime.instance
  prime.first n
end

first_n_primes("a")

I still think the example in the challenge was wrong, but then again I'm probably still missing something :stuck_out_tongue:


#4

I've looked around similar topics and it seems it's being discussed here too: https://discuss.codecademy.com/t/7-nice-work-the-return/22549/2

I'm happy I'm not the only one getting confused by this :slight_smile:


#5

Going back over the lesson I can see where I was mistaken. Implicit return applies to the last line of a function. return is indeed needed on both those lines. My apologies.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.