JavaScript JavaScript Basics Creating Reusable Code with Functions Create a max() Function

Elizabeth Almejo
Elizabeth Almejo
1,358 Points


Have tried a lot of variations, now trying things I don't think are correct. Can't move on, need help!

function max (2, 4) {
  if >2 return 4
  else return 4
return max;

1 Answer

Pro Student 51,087 Points

It appears you are trying to check a single set of values instead of creating a function that can handle multiple values. For that you will need to specify parameters that follow the naming conventions for variables:

From w3schools

The general rules for constructing names for variables (unique identifiers) are:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter
  • Names can also begin with $ and _ (but we will not use it in this tutorial)
  • Names are case sensitive (y and Y are different variables)
  • Reserved words (like JavaScript keywords) cannot be used as names

To keep it simple lets just call them num1 and num2

function max (num1, num2) {

Then you are kind of the right track in using an if statement but you will want to compare the two numbers

if (num1 > num2)

The instructions want you to return the larger of the two numbers so if the above is true return num1. Otherwise you can keep it simple and return num2.