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

Can someone help in this JavaScript code challenge please

Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

2 Answers

Let start by creating the max() function and pass it two arguments.

 function max(a,b) {}; // we're going to use 10 for a and 1 for b as the arguments pass to the function

Next compare a and b using the if/else statement. Is a greater than b? then return a or b.

  if(a>b) {
     return a;
}
   else {
   return b;
}

Finally, we call the max() function and pass the arguments to it which is 10 and 1 in this case.

     max(10,1)

Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

Hey Alexander, try to break it down to steps in descriptive English, that helped me alot.

For example:

-- Make a function named max(number1,number2) {

--check which number is larger with a condition if (number 1 is greater than number 2) { --return number1 } else { --return number2 }

};

Try to "translate" this to JavaScript and see if that helps :)