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

Andrea Algiers
Andrea Algiers
1,147 Points

Sending out an alert with 2 numbers from my argument...

I dont know what the alert is supposed to look like. I have tried multiply variations and have no clue what im doing wrong. :(

script.js
function max(num1, num2){ 
  if (num1 > num2) {
    return num1;}
  else if (num2 > num1) {
    return num2;}
}
alert( max(num1, num2));

3 Answers

Mladen Ignjatovic
Mladen Ignjatovic
13,602 Points

OK. then just :

 function max(num1, num2){ 
  if (num1 > num2) {
    return(num1);}
  else if (num2 > num1) {
    return(num2);}
}
alert(max(3,5));

3 and 5 is just for example you can put any numbers when you cal the function.

Andrea Algiers
Andrea Algiers
1,147 Points

omg. ok that worked. I kept putting num1, num2 bc those were the numbers i used up top. so that doesnt matter??

Mladen Ignjatovic
Mladen Ignjatovic
13,602 Points

Hi Andrea,

 function max(num1, num2){ 
  if (num1 > num2) {
    alert(num1);}
  else if (num2 > num1) {
    alert(num2);}
}

If num1 is larger, then alert box with(num1) will appear ,if num2 is larger alert(num2) will appear. happy coding

Andrea Algiers
Andrea Algiers
1,147 Points

hey! that wont work either bc they had me use the return function to return the proper number, now these are the next set of directions!

Beneath the max function you just created, call it with two numbers and display the results in an alert dialog. Pass the result of the function to the alert method.

For example, to display the results of the Math.random() method in an alert dialog you could type this: alert( Math.random( ) );

Mladen Ignjatovic
Mladen Ignjatovic
13,602 Points

num1 and num2 are just a arguments(something like placeholders) for real numbers you gonna put when you call the function. You could name them whatever you want , it doesn't matter. But you should try to give them a name in the context of a function, to make it easier for you.

You're welcome

Thank you for this explanation and posts. Helped me out :)