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

Stefan Cutajar
Stefan Cutajar
7,747 Points

function don't work

Hi all, I'm currently working on the following task: '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.' If someone could help me out would appreciate alot. Thanks, Stefan.

script.js
function max(number1, number2){
  var number1 = 256;
  var number2 = 512;
  if (number1 > number2) {
    return number1;
  }
  else if (number2 > number1){
    return number2;
}}
Stefan Cutajar
Stefan Cutajar
7,747 Points

Thank you both for your help I understand now. Regards, Stefan.

2 Answers

Elias Medina
Elias Medina
1,871 Points

Hi Stefan,

The function should be called by passing the two numbers to compare, in your case you are declaring two variables inside the function. Those values should be passed as arguments when calling the function instead of having them hard coded inside the function. This is what Josh meant by "the parameters are meant to be undefined until the function is called, that way it can be reused". If you leave the values hard coded, then the function will only work for that set of values and the idea of having a function is to have a reusable block of code that can be called with different values (arguments).

See the code below, it is your same function. But without the two numbers getting declared inside the function. And when we want to call the function we can pass any two numbers as arguments.

//This is the function max, and it takes two parameters number1 and number2
function max(number1, number2){
  if (number1 > number2) {
    return number1;
  }
  else if (number2 > number1){
    return number2; 
}}

//this is how we call that function and we pass two integers as arguments.
max(256,512);

//the output of the function call above will be 512, which means that the function returned number2 since it was the bigger value. 512 is greater that 256.
512

Hope this helps and happy coding!

Kind regards, Elias.

Josh Foster
Josh Foster
8,896 Points

You should never redefine number1 and number2 within the function, because you have already passed them in as undefined parameters. The parameters are meant to be undefined until the function is called; that way it can be reused: i.e., max(256,512);, or max(301,750); etc.