Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Brok Sorensen
Brok Sorensen
1,578 Points

How are you supposed to write the conditional statement for this one? Am i even close?

I am supposed to use a conditional statement to determine which number is larger and return that number. Please Help.

script.js
function max (number1, number2){
  return if(parseInt(number1) > parseInt(number2)){parseInt(number1)}
  else {parseInt(number2)}
}

2 Answers

Tiffany White
Tiffany White
5,373 Points

You are putting your return statements in the wrong place.

Ideally, you'd want the return statement after each conditional. I'll give you a head start.

function max (num1, num2) {
  if (...code...) {
   return // what will this return? Needs to return a number
  } else {
   return //if whatever number you have come up with isn't greater than the other, what should it return?
  }
}

You don't need the parseInt in this instance.

Brok Sorensen
Brok Sorensen
1,578 Points

Thanks Tiffany. That makes more sense.

Tiffany White
Tiffany White
5,373 Points

Brok Sorensen If my answer is good, could you accept it? I hope I've helped. Let me know if you have anything else.