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 function

Why will my code respond with correct answers when used with alert(); && document.write() but not for this challenge. Here is my code function max(num1, num2) { if (num1 > num2) { return num1; } else { alert("Keep trying"); } } max(23, 3);

Scott Montgomery
Scott Montgomery
23,242 Points

The challenge wants you to return the larger of the two numbers. If you were to give it the arguments (3,23) the alert would run instead of returning 23.

Your code works fine it just doesn't execute exactly what the challenge wants. Instead of alert('keep trying'); use return num2;

eddie duro
eddie duro
2,165 Points

The challenge is asking for the function to return a number. So your else condition doesn't complete the challenge. It should be num2 vs alert("keep trying");

eddie duro
eddie duro
2,165 Points

Exactly what Scott said, sorry didn't see you already answered!

1 Answer

Melissa Austin
Melissa Austin
3,383 Points

You are very close. Scott's response is correct. You just need to change the alert to return the largest number as shown below:

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