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

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Joshua Phillips
Joshua Phillips
3,429 Points

Function test

Trying to solve please help.

script.js
function max(red, blue) {
  var red = 20;
  var blue = 30;
 return answer;
}if (blue > red){
  answer = blue;
}

4 Answers

Steven Parker
Steven Parker
231,109 Points

:point_right: Here's a few observations:

  • the variables "red" and "blue" are passed to the function, you don't want to declare or assign them
  • you have a "return" before you have tested anything
  • you return "answer" which was never declared or assigned
  • your function ends (at the closing brace) before getting to your "if" statement
  • the "if" would be good if it was inside the function
  • inside the if you assign "answer" but you could just "return blue"
  • you still need to return the other value when the if test is not true

You might want to revisit the videos about function structure and passing arguments and returning values.

Joshua Phillips
Joshua Phillips
3,429 Points

I will see what I can do with what you said. I have rewatched those videos already and still having the same problems. I am more of a visual learner and lots of words don't help me too much.

Joshua Phillips
Joshua Phillips
3,429 Points

I found some place else that gave me a better answer. I used the if and else to return what I needed and got rid of the vars.

Cody Michaels
PLUS
Cody Michaels
Courses Plus Student 27,272 Points

Just so those checking this post later here is a formatted answer

function max(red, blue){
  if(red > blue){
    return red;
  }else{
    return blue;
  }
}