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

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.