8. Practice Makes Perfect string/integer error


#1


Hello! This is my first time posting. Having fun trying to figure out these challenges.

I'm at lesson 8:
Instruction: 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.)

The method is accepted but when I call the method, I get the error code: Oops, try again. Your code threw the following exception: can't convert String into Integer, and I'm not figuring out what I'm doing wrong.

Hopefully some other eyes will see what's amiss!

Many thanks.


def greeter(name)
    return "Hello, " *name
end

puts greeter("Joe")


#2

Hi Shawn, and welcome to the forums!

Your error says it can't convert a string into an integer. A string is basically words and an integer is basically a number, right?

Ok, so your error is telling you you're trying to turn your string into a number... i.e., there's some kind of unacceptable math operation going on here. Does this hint give you a push in the right direction?

frisby


#3

Look at 2nd line you multplying instead of "+"


#4

Thanks for the hint. Yah, I'm still getting my head to wrap around the idea of strings and math operations being in the same sentence, LOL.

So using + in this case is like saying (in English):
return the string "Hello" and the value of the parameter
yes?


#5

yes think like adding two string instead of multiplying


#6

Here is a starter... :

def greeter (name)
    print "Hey, #{name}!"
end

You will have to add another def though...
Hope it helps. :slight_smile:


#7

No problem! It can definitely be a little weird in the beginning.

Yes; if you ever had to play with these horrors in maths, you can imagine a piece of paper that says "Hello, " pasted on one block and name on the other.
To add them all you have to do is smash them together, but there is no clear route to multiplying one block by another (visually speaking).

Of course, iirc you can forgo all this by interpolating your variable into your string like so:

"Hello, #{name}"

such that it's all sort of contained within the string and you don't have to worry about having forgotten any spaces (e.g. "Hello, + name" --> Hello,Shawn). (*Update: as seen in @ayushisgr8's example above.)

frisby


#8

def greeter(name)
return "Hello #{name}"
end

def by_three?(n)
if n % 3 == 0
return true
else
return false
end
end


#9

(Make sure to use return and don't use print or puts.)


#10

Here's the correct code that worked for me:

def greeter(name)
return "Hello " + name
end
greeter("Eryl")

def by_three?(number)
if number % 3 == 0
return true
else
return false
end

end


#11

No need for "return true" it seems XD i never used IF loop, just entered:
return x % 3 == 0.
And if it is true it returns is like that :slight_smile:


#12

Should we add the lines to code to check if the condition is FALSE by using ELSE? Doesn't at the same time Ruby's operator == compare by default two operands IF they are equal or not (in another words it's operator's job to do)?


#13

why is there end end??


#14

one of the ends is for ending the if statement and the other is for ending of by_three? method


#15

Nice answer. As per the hint "It's a Ruby best practice to end method names that produce boolean values with a question mark." So there is no need to re-confirm if "by_three?" returns true or false with an if statement. IMHO. return number%3==0 will return either true or false given the method name ends with a question mark.


#18