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

Adding a conditional statement to a function.

Im stuck on how to add a condition that a instructs the function to pick the the larger number between two numbers.

script.js
function max(12, 13) {
  return max;
}
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Are you stuck on the syntax of an if statement?

2 Answers

Matthew Steele
Matthew Steele
2,598 Points

Inside the function you can call an if statement. So you could so something like this if I'm not mistaken:

function max(var1, var2) {
  if (var1 > var2) {
    return var1:
  } else {
    return var2;
  }
}

I'm not much of an expert on JS as I'm still learning but I think this should work.

jeffrey bachynski
jeffrey bachynski
5,179 Points

The question seems to be asking us to "Create a new function named max which accepts two numbers as arguments". Good job on naming the function (that part is works).

It seems where the question gets a bit confusing is the two numbers part. When you define the function you need to give the arguments names to be uses as parameters (they work like variables). You will give the function actual numbers later when you call it.

As for how to determine which is larger, there are many ways to do this, but, here, it hints to use " a conditional statement" for example an if ()... else statement. Then you can return the larger number based on the conditions that you test for.