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 trialMatthew Ashman
4,364 PointsFunctions: having a tough time.
"Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.
HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two"
Not quite sure what I'm supposed to be doing here.
Matthew Ashman
4,364 Pointscompleted stage with this code: but not sure why I needed or how I would implement the conditional statement. Any help?
function max (one, four) { return four; }
1 Answer
Nicholas Grenwalt
46,626 PointsYour arguments are the info that will be passed into your function when you call it at the end in an alert. They can be named anything you like..perhaps "num1" and "num2". Mine are "arg1" and "arg2". No matter the two numbers that get passed in you need to have a conditional statement that compares the two. In this instance an if-else statement seeing which number is greater. If "arg1" > "arg2" then "return" the "arg1" otherwise it will return "arg2".
We have the function all setup. Now inside an alert method call that max function and plug in any two numbers that you want. I did 3 (which will be plugged into the "arg1" spot) and 7 (which is plugged into "arg2").
So in my instance, 3 (arg1) is not greater than 7 (arg2) so 7 (arg2) will be what gets returned.
function max(arg1, arg2){
if(arg1 > arg2){
return arg1;
} else {
return arg2;
}
}
alert(max(3,7));
Hope this helped you grasp the challenge. Good coding to ya.
Matthew Ashman
4,364 PointsThank you for taking the time to explain this challenge out to me. Very helpful.
Matthew Ashman
4,364 PointsMatthew Ashman
4,364 PointsThis is what I came up with.
function max (one, four) { // my attempt at creating the function if (one > four) { // implementing the conditional statement return Better luck next time; //trying to fail the program if returns 1 } else if { // trying to set up the program to pass return Success!; } }
max (1,4);