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

Thaddeus Lorenz
Thaddeus Lorenz
3,434 Points

Can someone help me find the problem?

For some reason, my first task doesn't work after trying to complete the second task. Can someone please help me figure out what I'm doing wrong?

script.js
function max( a, b) {
 return (a,b);
  if (a > b)  {
  return max(a);
} else {
  return max(b);
}
}

3 Answers

The return in Javascript ends the function, so anything after your first return statement doesn't run. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

Thaddeus Lorenz
Thaddeus Lorenz
3,434 Points

How am I supposed to have both returns at the end of the entire function with an "else" separating the function?

Sean T. Unwin
Sean T. Unwin
28,690 Points

Within function max:

// pseudocode

// Conditional: Is a greater then b
// yes: return a
// no: return b

Just remove your return statement on line 2 and it should work.

function max(a, b) {
    if (a > b)  {
        return max(a);
    } else {
        return max(b);
    }
}
Thaddeus Lorenz
Thaddeus Lorenz
3,434 Points

Thank you for responding but it's saying that I don't have a function named 'max()' anymore.