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

Roscoe Coney
Roscoe Coney
13,124 Points

Challenge Having Diff4iculty

The Question asks:

Challenge Task 1 of 2

Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

What I have so far.

function max(num1, num2) {
  if (parseInt(num1) > parseInt(num2)){
  return true
  } else {
  return false;
  }
}

console.log (max (4,5) );

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Roscoe,

You have a couple of problems in your code.

  1. While not a direct problem that would cause an error you don't need to use parseInt since the function is already expecting an Number for both parameter values.

  2. Once you've completed the comparison you then need to return the number instead of a Boolean.

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

Hi Roscoe,

The function should return the larger of the two numbers. You're currently returning a boolean value.

So you need to either return num1 or num2 depending on how they compare.