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

Is the effective or correct?

function max(a, b) {
  if (a >= b) {
    return(a);
  }
  else {
    return(b);
  }
}

Mod note: Adding forum markdown on the code for clarity.

I had a code challenge and it said I passed with this, but I honestly was a little confused still.

2 Answers

It sometimes helps me to write it out! The function named "max" has two parameters a & b. If a is greater than or equal to b. Return value of a. Otherwise return the value of b.

function max(a, b) { if (a >= b) { return(a); } else { return(b); } }

Hope that helps.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points

Dakota is correct, but the function on its own doesn't really do anything. The functon as Dakota says has 2 parameter and these parameters have instructions to form.

In order to perform those instructions the function needs to be called with 2 arguments passed in.

Like this

max(2,5);