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

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,913 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, :)