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

Oladejo Oluwatobi A
Oladejo Oluwatobi A
1,443 Points

Help me with this code challenge, it seems impossible for me to break

This is my solution trying to create a function with the name max with two parameters, calling the parameters and testing which of the two numbers is larger

function max( small, big ) {

} max( 2, 4 ); if ( max === 2|| 4) { alert('This is the larger number'); } else { 'This is a small number' }

script.js
function max( small, big ) {

}
 max( 2, 4 );
  if ( max === 2|| 4) {
   alert('This is the larger number');
  } else {
   'This is a small number'
  }

2 Answers

Steven Parker
Steven Parker
231,275 Points

Here's a few hints:

  • the code for the function must be inside the braces of the function definition
  • the function code must compare the arguments with each other (by name)
  • the comparison must determine which item is greater (not equal)
  • the item determined to be greater must be passed back by a return statement
  • the function won't need to log or alert anything
  • the function will not reference itself or have any digits in it

I'll bet you can get it now without an explicit code spoiler.

Hi Oladejo, you are meant to have a conditional statement within your 'max' function to check which of the two numbers are larger and return the larger number in either instance.

function max( small, big ) {
   if (big > small) {
      return big;  // return big IF it is larger than small
   } else {
     return small;  // IF big isn't larger than small then small is the larger number, so small will be returned
   }
}

The second task in the challenge is to display the result of calling the 'max' function (by passing in two numbers as arguments) with an alert method.

alert(max( 2, 4 ));