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

How come my function does not return the higher number?

function max (x,y) {
  return large;
}
x = 10
y = 20
if (x>y) {
  large = x
}else if (x<y) {
  large = y
}

1 Answer

Hi Aviv,

Large is not defined here. So it has no idea what to do with it. If you wanted to make "large" a variable, you could do it like this:

var large;

This variable is emtpy, by the way. It doesn't hold anything.

But, in this case, there is no need for that. You can make a function that takes 2 parameters, we'll name them foo and bar. The function itself will check which one is the highest and returns it. If foo is higer than bar, it will return foo. If not, this means bar is always higer than foo and it will return bar.

function max(foo ,bar) {
    if (foo > bar) {
        return foo;
    } else {
      return bar;
    }
}