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

Multiple conditional statements

I am really having a hard time figuring this one out if anyone could help id appreciate it!

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.

script.js
function max () {

}

1 Answer

Ok... I'll try not to give you the answer directly, since the best way to learn programming is to try on your own, but I'll give you a few hints.

  • You should take in two arguments: a and b. You can call them whatever you want, though, as long as it is a valid variable name. For example, they can't contain spaces!
  • You can make an if/else clause. The condition should check if a is bigger then b.
  • If a is bigger than b, return a.
  • Otherwise, return b. Tip: Otherwise means the same thing as else!

I hope these hints help :grin:

If you need more help, post the code that you tried below and I'll try to help more.

Happy coding! :tada:

:dizzy: ~Alex :dizzy:

Would there be any specific way you would start this piece of code?? I took your and used a & b as my arguments. But later when i get to my if/else clause I try to use 2 numbers. Long story short I am apparently not doing it right haha. Any help is appreciated.

Here's a big hint...

function max(a, b) {
  if ( /* Is a bigger than b? */ ) {
    // Return a
  } else {
    // Return b
  }
}