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

Andrew Nicholson
Andrew Nicholson
2,474 Points

This makes me want to give up

I really am having a good deal of trouble understanding how to make this work, even after going over and over the video and transcript...

script.js
function max() {
  var larger = (if else (25 > 15));
  return large;
}
alert = (larger(25, 15));

2 Answers

Mark VonGyer
Mark VonGyer
21,239 Points

Hi Andrew!

Don't give up!

There are a few conditional commands: If, else if, else.

Best practice is to include {} after the conditional statement (which would be inside () after the conditional command).

So if(25>15){return 25}else{return15}.

Another issue is that you need to pass the variables into the function.

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

your code makes absolutely 0 sense, have you even being paying attention at all to the lessons?

function larger(a,b) {
    return a===b ? "equal" : a > b ? a : b;  
}

console.log(larger(4,3));
Andrew Nicholson
Andrew Nicholson
2,474 Points

No, I've been sitting up the back, writing graffiti on the tables and chewing gum.