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

javascript basis create max() part 2

I would greatly appreciate if someone could help me get through and understand this challenge. Thanks to anyone willing to give their time and knowledge.

script.js
function max(a, b){
  if (a > b){
    return a;
  }
  else{
    return b;
  }
}
alert(math.random(2, 10));

1 Answer

Hi Brandon,

You are very close with your code! Inside of the alert command, you want to make a call to your max function instead of Math.random. Just replace Math.random with max. The challenge uses Math.random() inside of the alert command as an example of how to call a method or function.

Also, the else in this case is optional because once a function returns a value, the function stops. So, if the if statement doesn't execute, that means b is greater than a and so it will return b.

function max(a, b){
  if (a > b){
    return a;
  }
    return b;
}
alert(max(2, 10));