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

not sure what i'm doing wrong here

Not sure what i'm doing wrong here

script.js
a=1;
b=2;

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

Thanks Christian

2 Answers

Hey

Almost

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

You return the variable a or b not the value of them

Hope this helps

Happy coding

Paul

Thanks Paul

3 mistakes:

  • wrong function syntax
  • your logic works only if a number is bigger than the other, not the other way round
  • always returning the same number instead of the input

Answer:

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

This function:

  • Takes two numbers, the first input will be assigned to a while the second is assigned to b
  • A condition checks if a>b, if so, a is returned
  • An else catches the if, and will return b

Although this passes the requirements of your exercise, this has a glitch of always returning 'a' if 'a' and 'b' are equal.