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 trialGena Israel
2,047 Pointsconfused how to put conditional statement in return
function max(5, 7) { } return(
function max(5, 7) {
}
return(
1 Answer
Umesh Ravji
42,386 PointsHi Gena, return statements should be inside your function. Please note that parameters shouldn't be hard coded values, they will be supplied to the function when the function is called.
In this case the two parameters are a and b, but you can name them almost anything you want. A conditional check is done to see which is smaller, and then that number is returned. I'm sure you can modify this to what you need.
function min(a, b) {
if (a < b) {
return a;
} else {
return b;
}
}
min(5, 7); // returns 5
Gena Israel
2,047 PointsGena Israel
2,047 Pointsim confused by your use of brackets and parenthesis here. if the return statement is inside the function....
Umesh Ravji
42,386 PointsUmesh Ravji
42,386 PointsHi Gena, let me try and explain it to you. I'm not sure I completely understand your question, so if this doesn't help, let me know :)
The first conditional check
(a < b)
to see ifa
is less thanb
. If this is true, the function returnsa
. If the first condition is false, the else branch will be taken, and the function will returnb
.Alternatively the function could be written with a single return statement: