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 trial

Ruby Ruby Basics (Retired) Ruby Methods Method Returns: Part 2

Dino Tudor
Dino Tudor
1,767 Points

Help to solve the code challenge, defining methods

Hello, im having a hard time with the code challenge below. How can i set the value to the "c" and insert it to the sentence? Thanks!!

Challenge Task 1 of 1 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.

method.rb
def mod(a, b)
  puts "The remainder of #{a} divided by #{b} is #{c}." 
  return a % b
end

mod(10, 2)

2 Answers

Laura Long
Laura Long
5,533 Points

Hi there, I think the wording of the problem is tripping you up. Adding "c" into the equation as a variable will not work because you have not assigned it a value yet. The solution is to get rid of the letter "c" altogether and just plug in the value, "a%b" instead.

def mod(a,b)
   return "The remainder of #{a} divided by #{b} is #{a%b}."
end

Alternatively, if you really want to use "c" as a variable, assign it the value of a%b before calling "c" in the string.

def mod(a,b)
   c = a % b
   return "The remainder of #{a} divided by #{b} is #{c}."
end
Dino Tudor
Dino Tudor
1,767 Points

Thanks!! Great help Laura!

Try this:

def mod(a, b)
  c = a % b
  return "The remainder of #{a} divided by #{b} is #{c}."
end

First, you don't need to call the function (in your code). The code challenge does that automatically. Second, the variable c doesn't exist (in your code). Lastly, you don't need to print anything. All you need to do is to return the valid string (in your... OK, I think you know what I'm about to say).

I hope this helps. ~Alex

Dino Tudor
Dino Tudor
1,767 Points

Thanks Alex! Got it!