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

Manveer Sandhu
PLUS
Manveer Sandhu
Courses Plus Student 1,389 Points

creates a function with the if and else condition

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

script.js
function max ( one , two) {
 if (one > two){
  var numbers = 

 }

2 Answers

Christopher De Lette
PLUS
Christopher De Lette
Courses Plus Student 7,139 Points

Hi Manveer Sandhu, In this challenge, the main goal is to understand how to use conditional statements and pass arguments to a method. You have the idea correct you are simply overthinking it. Simply write the function with two arguments in the max function, use a conditional if/else statement to test larger of the two numbers, and return the larger of the two being compared. So the following JS snippet is how i would code this challenge:

function max(x, y) {
   if (x > y) {
   return x;
  }
   else {
   return y;
  }
}

Hope this helps and Happy Coding!

Alex Forseth
Alex Forseth
8,017 Points

Hello Christopher. Your second sentence in response to manveer gave me great relief as I have been pretty hard on myself for not being able to figure out the same challenge

That being said what trips me up as a newbie coder is for example "function max(x, y)" <-----When x and y become described as an 'argument'. Aren't they parameters because they are on the first line of the function? Hope this makes sense.

Thanks.

Christopher De Lette
PLUS
Christopher De Lette
Courses Plus Student 7,139 Points

Hi Alex,

It is correct to state, and my mistake, to call the input variables parameters when formally setting up a function or method. As far as positional reference (first line of function) that is something to ask a way more experienced OOP programmer.

Take care, Chris