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

Todd Harvey
Todd Harvey
3,178 Points

just need toe answer here please

Need answers why here

script.js
function max(one,two){
  if (one > two) {
    return one;
  }else if (two > one){
    return two;
}
}

2 Answers

Steven Parker
Steven Parker
231,236 Points

You're very close, but you should still cover the case where both values are the same. You could do this by adding another "return" at the very end.

But another way would be if your first test does not pass, you can just return the other value right away without another test.

Once you pass task 1, then just call your function with any 2 values from inside an "alert" function call.

Todd Harvey
Todd Harvey
3,178 Points

i had to type alert(max(2,3));

Robert Stewart
Robert Stewart
11,921 Points

I just posted this answer in your other topic about the same issue:

I just tried your solution and it worked. Which browser are you using? It may be a stretch, but since ES6 is not supported on all browsers it may not be able to run your code to validate it since you're using ES6 syntax.

Try this and see if it works, this is in the old syntax:

var max = function(one, two) {
  if (one > two) {
    return one;
  } else if (two > one) {
    return two;
  }
}

As a side note you could also do:

function max(one, two) {
  if (one > two) {
    return one;
  }

  return two;
}

Since the first return statement stops the function from going any further the second return statement will only execute if one is not greater than two. (Makes your code a lot easier to read in the long run)

Todd Harvey
Todd Harvey
3,178 Points

i got it. I had to type alert(max(2,3));