Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

kabirdas
kabirdas
1,976 Points

Help with conditional statements and returning a function

Hi,

Can I please have some help with this code?

The instructions are to create a new function named, "max," and return the larger number, using a conditional statement to test them.

I'm not sure how exactly the syntax is for this? Can someone please help?

Thanks

KD

script.js
function max(1,2) {
  if (1 < 2)
  return 2;
} 

2 Answers

Hi Kabir

function max(x, y) {
  if ( x > y) {
    return (x); 
  } else {
    return (y); 
  }
}
John Paul Naranjo
seal-mask
.a{fill-rule:evenodd;}techdegree
John Paul Naranjo
Full Stack JavaScript Techdegree Student 2,090 Points

On this challenge, you need to create a "max" function that you can use again and again to find the largest between 2 number comparison ( whatever these numbers maybe) ( like a Chris Jardine's code above). Yours is not "reusable" because you hard coded the values into the function instead of passing them through the function's argument and parameters (x, y) The point of creating a function is make them once and use them as many times as you want.

Suggestion: You need to also know what function 'arguments and parameters' are before taking on this challenge.

kabirdas
kabirdas
1,976 Points

It wasn't very clear that was the way they wanted us to set up the questions. what I was missing was the else and return clause. I re-did the challenge using numbers instead of "x,y" (or whatever it can be named" and it came out correct. However, once I would have gotten to the second part of the challenge it was clear that it needed to be done in this way. Thanks.