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

Shauna B

I feel like i am missing something but i don't know what?

script.js
function max (2,5) {
  if ( 2< 5) {greeting = "test1";} else {greeting = "test2";}
  return larger;
}

2 Answers

Hey Shauna,

Whenever you write functions, you want to use dynamic pieces of code that can be used later on with different values. So, if you look below, I specified two variables to be used as placeholders for the function: num1 and num2. You wouldn't want to use numbers in your function max (2,5) beginning because then the only arguments can be 2 and 5. If you instead use placeholder variables, you can put anything you'd like into the function!

The function checks to see if num1 is greater than num2 and if it is, it returns num1. And then I put return num2; outside of the if-statement because that return statement will only execute if the first return statement did not execute.

function max (num1,num2) {
  if (num1 > num2) {
    return num1;
  }
  //Only executes if the if statement didn't evaluate to true
  return num2;
}

And then in the 2nd task, you're asked to put the results of the function into an alert box. So, all you need to do is place any two numbers as arguments to the function max() within the alert() command:

//2 and 5 can be any number you'd like
alert(max(2,5));
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Shauna.

The code challenge wants you to create a function that takes 2 numbers and returns the larger between those two.

We do not want to hard code the input: this means when a code challenge asks you for something like two numbers, we need to pass in 2 generic numbers, like x and y.

Then, the function will need to check which number is greater if x or y and we will have 2 cases: if x > y we return x, else we return y

Let me know if this helps!

Vittorio