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 trialJoshua Phillips
3,429 Pointsfunction
This is the question that I need to solve. I am having trouble solving this. Please show me how you would solve it and I can use that for my notes to remember.
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.
4 Answers
Steven Parker
231,269 PointsIt doesn't look like you've written any code yet. Give it your best try, and if you're still stuck, share your code so far and someone can help you with it.
If you really have no idea how to start, you may want to review the video about functions or the ones about giving and getting information from functions.
Joshua Phillips
3,429 PointsI have reviewed the videos and trying to piece it all together I'm not getting it.
Joshua Phillips
3,429 PointsThis is my code that I'm stuck on
function max(red, blue) { var red = 20; var blue = 30; return answer; }if (blue > red){ answer = blue; }
Joshua Phillips
3,429 PointsCan anyone help please?
Thomas Nilsen
14,957 PointsThis was your code:
function max(red, blue) {
var red = 20;
var blue = 30;
return answer;
}
if (blue > red) {
answer = blue;
}
The answer is easier than you think.
You're supposed to make a function (just make it, not call it) that accepts to arguments. If the first argument is greater than the other, you return that. Otherwise you return the second argument.
That would look like this:
function max(red, blue) {
if (red > blue) {
return red;
} else {
return blue;
}
}