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

Michael Lawinger
Michael Lawinger
33,581 Points

Function Conditional Statement

The question in the challenge is: 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.

Where do I go from the spot I am now, I'm struggling with the logic in general.

script.js
function max(6, 3) {
  if ( 6 > 3 ) {
    return true;
  } else {
    return false
  }
}
Vlad Legkowski
Vlad Legkowski
9,882 Points

The answer is here - function named max which accepts two numbers as arguments

2 Answers

Michael Lawinger
Michael Lawinger
33,581 Points

Thank you! The explanation was very helpful, I got the concept now!

Michael Lawinger
Michael Lawinger
33,581 Points

Sorry could I get a little more help I'm still pretty confused.

Vlad Legkowski
Vlad Legkowski
9,882 Points

you can name the arguments, whatever you would like - you ever named anything as a number? I guess no, in JS you normally name something as number1, number2 and so on.

Example:

var number1 = 6;
var number2 = 3;

The function should return the larger of the two numbers - the task is not asking you to evaluate if the condition is true or false. It wants you to return the largest number if the condition is true.

Bellow is the answer, if I wasn't clear enough

function max(num1, num2) {
  if ( num1 > num2 ) {
    return num1;
  } else {
    return num2;
  }
}