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

confused how to put conditional statement in return

function max(5, 7) { } return(

script.js
function max(5, 7) {
}
return(

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Gena, return statements should be inside your function. Please note that parameters shouldn't be hard coded values, they will be supplied to the function when the function is called.

In this case the two parameters are a and b, but you can name them almost anything you want. A conditional check is done to see which is smaller, and then that number is returned. I'm sure you can modify this to what you need.

function min(a, b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}

min(5, 7); // returns 5

im confused by your use of brackets and parenthesis here. if the return statement is inside the function....

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Gena, let me try and explain it to you. I'm not sure I completely understand your question, so if this doesn't help, let me know :)

The first conditional check (a < b) to see if a is less than b. If this is true, the function returns a. If the first condition is false, the else branch will be taken, and the function will return b.

function min(a, b) {
    // everything inside here is inside the function and will
    // be run when the function is called.
}

Alternatively the function could be written with a single return statement:

function min(a, b) {
  var smaller;
  if (a < b) {
    smaller = a;
  } else {
    smaller = b;
  }
  return smaller;
}