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

Curtis Simonson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Curtis Simonson
UX Design Techdegree Graduate 13,791 Points

Create a Max() Function

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

I have been stuck on this for awhile. I keep getting a parse error. I tried using the parseInt and I keep getting the same error. How do I correct it?

script.js
function max ( big, little){
  var equality =  parseInt(big * little);
  return equality; 
}

  if max parseInt(2, 1){
      "correct";
  } else max parseInt(2, 1){
    "incorrect";
 }

3 Answers

Curtis Simonson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Curtis Simonson
UX Design Techdegree Graduate 13,791 Points
function max ( ){


    if max (big, little){


     big > little;
      return big;


  } else max (big, little){


      big < little;



return little;


 }

}

  max(2, 1);```


I kept getting the same error
Steven Parker
Steven Parker
231,096 Points

You almost have the formatting down. The 3 backticks need to be on thier own lines, but the first one can have "js" after for syntax coloring.

I'll repeat the things that vanished a few minutes ago:

  • the parameters must be named in the function declaration (top line)
  • the function name will not be part of the conditional
  • the comparison expression should go inside the parentheses after the "if"
  • the "else" doesn't use any expression, it handles the "left over" cases automatically
Steven Parker
Steven Parker
231,096 Points

You won't need to use parseInt for this challenge, it's only for converting strings into numbers. You can assume both of the arguments given will be numbers already..

Other things to keep in mind:

  • the conditional statement should be inside the function
  • the function will always return one of the arguments passed in (the largest one)
  • you won't need any variables other than the parameters
Steven Parker
Steven Parker
231,096 Points

Did you implement the other hints as well? If you still have issues, remember to update the question to show what the code looks like now after the changes.