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 Functions Pass Information Into Functions Create a max() Function

This prompt isn't making sense, my code works and it won't pass?

script.js
function max (num1, num2) {
  if (num1 > num2) {
    larger === num1
  } else (num2 > num1) {
    larger === num2
  }

  return larger;

}

I tried this version too:

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

2 Answers

Your second version is closer in that you now:

  • declare variable larger. However many of the challenges don't recognize const or let and I'm not sure you'd use const there anyway. Try var or better yet eliminate the variable and just return the value.
  • use an assignment operator when assigning num1 and num2

But you have one additional thing to change

  • else shouldn't be followed by a condition. It is what executes when all previous conditions are false.
Steven Parker
Steven Parker
229,982 Points

When you say "it works", how did you test it? At first glance I see these issues:

  • the variable "larger" is never declared or assigned any value
  • the "===" operator makes a comparison, use a single "=" to perform an assignment
  • an "else" statement does not take a conditional expression (nor would it need one)