Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Unable to complete this task

I am trying with the below code:

function max( 1, 2 ) { if ( 1 < 2 ) { return 2; } else { return 1; } }

script.js
function max( 1, 2 ) {
  if ( 1 < 2 ) {
    return 2; 
  } else {
    return 1; 
  }
}

4 Answers

Michael Hulet
Michael Hulet
47,869 Points

Although I'm not sure what the task does, I bet I can guess what's going wrong. With your code, JavaScript will evaluate the numbers literally, instead of as variable. In other words, you can't use just numbers to name variables in JavaScript. Try this code:

function max(first, second){
  if(first < second){
    return second; 
  }
  else{
    return first; 
  }
}
function max( a, b ) {
  if ( a < b ) {
    return b; 
  } else {
    return a; 
  }
}
huckleberry
huckleberry
14,636 Points

You're using numbers within the parameters and you're most likely getting a syntax error. Avoid using numbers as your parameters.

Here's another version that will always return the bigger of the two numbers.

//Function set to always return the biggest number of a pair
function max(num1,num2){
  var bigger;

  if (num1 > num2){
    bigger = num1;
    return bigger;
  }
   else {
    bigger = num2;
    return bigger;
  }
}

//Calling the function and displaying it with an alert

alert(max(13,45));

Thanks folks, :)