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 trialSahmad Nakumbe
3,423 PointsCreate a function named max that accepts two numbers as arguments (name the parameters whatever you would like). The fun
function max(a1, a2) {
if {
} else {
return(a1 + a2);
}
numbers();
2 Answers
Mark Tripney
8,666 PointsHi Sahmad! I'm guessing you're having trouble solving this one? The script you've posted isn't actually doing anything. For one, you haven't passed any arguments to the function, and there's no condition in your if
statement...
function max(a1, a2) {
if (a1 > a2) {
return a1;
} else {
return a2;
}
console.log(max(4, 6));
Or, for a more succinct solution, using a ternary operator, you could try:
function max(a1, a2) {
return a1 > a2 ? a1 : a2;
}
console.log(max(4, 6));
The return statement here looks quite complicated, but really isn't. The first part, a1 > a2
is the condition we're testing then, following the question mark, we have two possible outcomes. If the condition is truthy, the expression left of the colon is executed; if it's falsy, the condition right of the colon is executed.
Sahmad Nakumbe
3,423 PointsThank you