Practice Makes Perfect (9)


#1

Assignment is:

Define two methods in the editor:
1. A greeter method that takes a single string parameter, name, and returns a string greeting that person. (Make sure to use return and don't use print or puts.)

It reads the code below as correct, but i want it to show "Hello John" at the end, without using puts or print. If i add puts greeter?("John") at the end it does return that but then it shows an error :confused:

The second part with the numbers works fine, it returns "false" but that is the only thing. Is this exercise flawed or maybe it doesn't need to show the users greeting ?


https://www.codecademy.com/courses/ruby-beginner-en-ET4bU/1/4?curriculum_id=5059f8619189a5000201fbcb#


def greeter(name)
    return name
end

greeter("Mike")

def by_three?(x)
   return x % 3 == 0
end

by_three?(5)


#2

so...it occurred to me to delete the method for numbers and focus on the first one, and it works fine :open_mouth:

I guess it cannot return 2 values or is there something else i need to do so it returns

"Hello Mike"
false

?


#3

You've got to keep apart return and print/puts, they do very different things.

If you don't put/print then you aren't sending anything to the console and your code will not be causing anything to show.

Codecademy for some reason prints out the result of the last expression or some such, but that's not your code and frankly you should ignore whatever that prints.

If you want something to show, then use puts/print. Wanting to make something show without showing it, won't really get you anywhere. (you'd just have to find something else that is equivalent, but then what's the point of avoiding puts/print)

Methods only return one value, but that's easily worked around by putting them in an array or other data structure. A greeter method probably doesn't have any reason to return more than just one value though, the string it creates. You can certainly think of a string as a container for multiple characters.


#4

Tnx man, totally makes sense now :slight_smile: They just confused me a bit with "do not use puts/print" thing :smile:

Tnx for the fast clarification !


#5

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