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

Lance Volk
seal-mask
.a{fill-rule:evenodd;}techdegree
Lance Volk
Full Stack JavaScript Techdegree Student 1,628 Points

I cannot seem to get the right code for this challange

I am not positive about this conditional statement, it seems that this particular problem was not addressed in the videos or my syntax is lacking, I have tried different variations using parseInt. please help

script.js
function max (num1, num2) {
 if {(num1) > (num2) alert (num1);}
   else {alert(num2);}
         return max;}
  console.log max(20, 30);

2 Answers

Steven Parker
Steven Parker
231,108 Points

:point_right: You have an undefined variable, some misplaced braces and missing parentheses.

You also have a bunch of unnecessary parentheses and alert statements. The function name "max" has no special meaning inside the function, and in particular cannot be used as a variable (but you could declare one with that name if you wanted).

A more condensed and corrected function might look like this:

function max(num1, num2) {
    if (num1 > num2)  // the conditional expression must be in parentheses
        return num1;  // a single statement needs no braces
    return num2;      // ELSE is not needed when the IF does a RETURN
}

If you'd like to review, conditional statements were introduced in the previous module in this video.

Lance Volk
seal-mask
.a{fill-rule:evenodd;}techdegree
Lance Volk
Full Stack JavaScript Techdegree Student 1,628 Points

Thanks Steven, I thought that I was over thinking the problem, the two returns kind of threw me, but it ended up being quite logical, I will review some of the videos to see what I missed. Thanks again Lance