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

Can someone please explain how to use conditional statements with parameters and arguments?

I'm hoping someone can walk me through this javascript challenge, I understand conditional statements, however I'm having a bit of trouble trying to use them with parameters and arguments. "Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two."

Brandon,

Do you understand this a bit better now, or would you like further elaboration?

Erik

1 Answer

This is what I would do:

function max(nbr1, nbr2) {
  if (nbr1 > nbr2) {
    return nbr1;
  } else {
      return nbr2;
  }
  }

var maxNbr = max (87, 23);
document.write (maxNbr);

The function accepts the two numbers: nbr1 (87, in this example) and nbr2 (23, in this example). 87 is not greater than 23 so we fall into the 'else' code, and the function returns 87. I've set up a variable maxNbr to store the 87 returned by the function. And then, I write it to the page.

Does that help?

MOD edit: Changed from "Comment" to "Answer", because it is an answer and can now be up-voted and/or marked as Best Answer. Also added Mark Down to make code more readable.