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

Hunter Shaw
Hunter Shaw
2,187 Points

Trying to get the larger of the two? I don't understand this.

Help with my program to get the larger number of the two.

script.js
function max(num1, num2) {
  var numbers = num1;
  numbers += num2;
  if (num1 > num2) {
   document.write(num1);
} else {
  document.write(num2); 
}
  return numbers;
}
max(5, 7);

1 Answer

Steven Parker
Steven Parker
231,236 Points

You're close. But you don't need to add the numbers together, and you don't need the "numbers" variable. Also, the function should return the largest value (not "document.write" it).

The for task 2, when you invoke the function it should be inside an "alert()" call.