Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,660 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.