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

Need an answer

I've been trying to figure out what's wrong with my code but I can't figure it out. Can someone please help me figure out where I'm going wrong IN the problem?

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

1 Answer

Julian Gutierrez
Julian Gutierrez
19,201 Points

A couple of things, first of all there is no need to use Math, that was just an example given in the question to demonstrate how to use alert(). Your if else statement needs to go inside of your function, again no need to use Math here. Outside of your function call your function using two numbers inside of alert() as shown below.

//Calling the function inside of alert
alert(max(1,2));
Thaddeus Lorenz
Thaddeus Lorenz
3,434 Points

Thanks for responding but when I did everything it didn't work for some reason. Here's the code I just did: function max( a,b ) { return max; if ( a > b) { alert(max( a>b )); } else { alert(max( a<=b ); } alert(max(1,2));