Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel O'Rourke
3,100 PointsProblem getting coding question correct for Ruby basic intro
Here is the question: In the previous challenge, we wrote a method that returned the remainder of two arguments when divided. That's cool, but it would be nicer if the method returned the remainder in a nice full sentence. Use string interpolation to return the remainder inside the sentence “The remainder of a divided by b is c.” where a is your “a” variable, b is your “b” variable, and c is the value of a % b.
I write the code: def mod(a, b) c = a % b puts "The remainder of #{a} divided by #{b} is #{c}." end
puts mod(4, 3)
This seems to work fine in the shell on the Treehouse workspace, but whatever I seem to put I get an error that my string is incorrect. I'm not sure how to proceed.
6 Answers
Marc Schultz
23,356 PointsThe thing you missed in the objective description is:
... but it would be nicer if the method returned the remainder in a nice full sentence.
Returning means to use return
instead of puts
. Which also means the return type is a string.
def mod(a, b)
c = a % b
return "The remainder of #{a} divided by #{b} is #{c}."
end
Gavin Ralston
28,770 PointsInstead of using puts, just return the string from the method.
So just the string itself, or add the "return" keyword is all you have to do there. :)
Daniel O'Rourke
3,100 PointsThanks that fixed it :)!!
Stephen Hanlon
11,845 PointsAnd while this will return a similar answer it's not what the challenge is asking for...correct?
def mod(a, b) puts “The remainder of #{a} divided by #{b} is:” return a % b end
puts mod(3, 2)
Stephen Hanlon
11,845 PointsI find that when using the # sign in the Challenge, my answer turns into a Ruby comment and then after submitting, registers with Syntax errors. The code used above by Marc when copy and pasted doesn't have this problem with the # sign. What's the magic?
Bummer! SyntaxError: 23034d9b-3cf3-4022-91cd-4989f2a5f92d.rb:9: invalid multibyte char (US-ASCII) 23034d9b-3cf3-4022-91cd-4989f2a5f92d.rb:9: syntax error, unexpected end-of-input, expecting keyword_end return “The remainder of #{a} divided by #{b} is #{c}.” ^
Marc Schultz
23,356 PointsThe # sign is for commenting purpose, right. But in case of a string, the # sign in comination with curly braces works a different way.
# this is just a comment
a = 3
puts "This string prints the variable a and its value #{a}."
Marc Schultz
23,356 PointsCan you post your code here please?
Stephen Hanlon
11,845 PointsThanks, Marc, for clarifying that. What I'm saying is that the Code Challenge editor is commenting out everything after #{a} as in "...#{a} divided by #{b} is #{c}. I can tell because comments are italicized grey text and that's how it appears in the editor. So even if I type the correct answer it's coming up as a syntax error. I guess if no one else is having this experience I'm typing something wrong.