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

Larissa Röhrig
Larissa Röhrig
1,787 Points

My code doesn't work and I really don't know why.....Please help!

I red a discussion about this exercise and I did everything that was mentioned there. But my code doesn't work and I get the parse error.

Here is my code:

function max(50,10) {
 if (50>10) {
  return 50;
 }
else {
return 10;
}
}
alert( max(50,10) );
script.js
function max(50,10) {
 if (50>10) {
  return 50;
 }
else {
return 10;
}
}
alert( max(50,10) );

1 Answer

Benjamin Larson
Benjamin Larson
34,055 Points

Hi Larissa,

You've done something called hard-coding, which is just a way of saying that the code is kind of fixed to only work with very specific data that you have pre-defined. In this case, your code won't run, but if it did, your logic is spot on! The problem is that, while this would solve which of 50 and 10 is larger, it wouldn't be of much use to test anything else...which isn't very useful.

The challenge says that the function should accept two arguments, that can be named whatever you like. So you might see something like this:

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

Or alternatively:

function max(firstNumber,secondNumber) {
  if (firstNumber > secondNumber) {
    return firstNumber;
  }
  else {
    return secondNumber;
  }
}

As part of the function definition, firstNumber and secondNumber are called the ARGUMENTS. But when you call the function, using your example:

max(50,10)

then, 50 and 10 are the PARAMETERS. They are similar, but the distinction is significant here.

But now, you can input whatever numbers you would like when you call the function, without having to change the value multiple times in the function definition. Way more flexible, way more powerful, way more efficient :D

Larissa Röhrig
Larissa Röhrig
1,787 Points

Thank You Benjamin! Your answer was very helpful!!