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

Ok now I'm certainly lost!

Hi,

I've tried going over my notes and even hunting the internet to help solve this challenge but at no point do i remember going over using conditionals with a function ......

Any help appreciated on this one , and i didn't know until to day so ill just mention it if you click the file icon with script.js it takes you to the challenge rather than me write it out...

Thanks anyone who tries to help!

Craig

script.js

1 Answer

Hey Craig!

This is the solution!

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

alert(max(20, 30));

Feel free to ask any questions about it if you don't understand it.

-Luke

Hi Luke,

Thanks for your help, using your code task one passes but task two does not.. , after looking at your code in responses to task one it now see where i was going wrong , on task two it actually gives you the code needed with the hint to change math.random to max but then the task fails ..

any ideas ?

here's code I used on this challenge

function max(n1,n2) {
  return Math.max(n1,n2);

  // or

  // return n1 > n2 ? n1 : n2;

  // or

  // as Luke shows above, with if/else
}
alert(max(1,2));

Hey Craig!

Try out the code that I pasted in earlier! I updated it so that it will pass the second challenge as well!