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

Nikita Savelev
seal-mask
.a{fill-rule:evenodd;}techdegree
Nikita Savelev
Full Stack JavaScript Techdegree Student 3,741 Points

The question is confusing and i'm not sure exactly how to work it out

not exactly sure what to do here and every result is wrong.

script.js
function max( upper , lower){
var high = upper>lower
var low  = lower>upper
if (high == 15){

    }
}
console.log(max(15 , 5))

2 Answers

Adam Pengh
Adam Pengh
29,881 Points

You need to use a conditional if statement to check which of the values passed in is largest.

function max(a, b) {
  if(a > b) {
    return a;
  } else if(b >= a) {
    return b;
  }
}

alert( max(2, 5) );
Steven Parker
Steven Parker
231,271 Points

Perhaps these hints will help:

  • you won't need to create any new variables
  • don't look for any specific values inside the function
  • in your conditional ("if") test, be sure to compare the parameters against each other
  • when you determine one is larger, you must return that parameter
  • if the test fails, you must return the other parameter
  • you won't need an "else" or "else if"