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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Ryan Roy](https://secure.gravatar.com/avatar/4de37794311f99ab3dcadc3f2e2533fa?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Ryan Roy
Courses Plus Student 3,586 PointsCreate a new function named max() which accepts two numbers as arguments. The function should return the larger of the 2
Having trouble with this one the other comments I found on the site we're not helpful to me.
function max(a,b){ var first = a; var second = b; if first > second { return("first is greater"); } else { return("second is greater"); } max(4,5)
function max(a,b){
var first = a;
var second = b;
if first > second {
return("first is greater");
} else {
return("second is greater");
}
max(4,5)
6 Answers
![Brian Romine](https://uploads.teamtreehouse.com/production/profile-photos/2684602/micro_avatar.jpg)
Brian Romine
9,518 PointsI can see a couple issues in your code. The first is a syntax error.
foo = "bar";
if ( foo == "bar" )
{
foo = "something";
}
is the general syntax for an If statement. You will need to add (first > second) for your condition test.
Second, a style issue. Why define the var first and the var second? Would it work to just test if (a > b) ?
Third, I see a minor logic issue. What about an edge case where a === 3 and b === 3? You'll get a result that the second is greater, maybe add an "else if" clause? (that might be beyond the scope of your challenge)
![Ryan Field](https://uploads.teamtreehouse.com/production/profile-photos/2892162/micro_ryan3.jpg)
Ryan Field
Courses Plus Student 21,242 PointsIn addition to what Brian has said, you are missing a final closing curly brace (}
) for your function, which will cause an error.
![Julian Gutierrez](https://uploads.teamtreehouse.com/production/profile-photos/97232/micro_jg-logo2.jpg)
Julian Gutierrez
19,201 PointsLike mentioned above you need a closing bracket for your function but the challenge also need you to return the largest of the two numbers not a string. You would need to return a or return b.
![Ryan Roy](https://secure.gravatar.com/avatar/4de37794311f99ab3dcadc3f2e2533fa?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Ryan Roy
Courses Plus Student 3,586 PointsI found this was the solution that worked for me. Yes I was missing the extra bracket and returning a string. I may have been over thinking the question. Thanks for all your help guys. I really appreciate the support.
function max(num1, num2) { if (num1 > num2) { return num1; } else{ return num2; } }
![Brian Romine](https://uploads.teamtreehouse.com/production/profile-photos/2684602/micro_avatar.jpg)
Brian Romine
9,518 PointsGood job!
![Brian Romine](https://uploads.teamtreehouse.com/production/profile-photos/2684602/micro_avatar.jpg)
Brian Romine
9,518 Pointsfunction max(a,b) {
if (a > b)
{
return("first is greater");
}
else
{
return("second is greater");
}
max(4,5);
Still two issues. Look carefully at your brackets. You need a closing bracket for every opening bracket. Also, like Julian said, you should be returning the larger number.
![Ryan Roy](https://secure.gravatar.com/avatar/4de37794311f99ab3dcadc3f2e2533fa?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Ryan Roy
Courses Plus Student 3,586 PointsThanks guys, This is where I am now based on the comments. It's still not passing however.
Here is the question again so you don't have to scroll up: Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.
function max(a,b){ if (a > b) { return("first is greater"); } else { return("second is greater"); } max(4,5);