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
Will Peach
6,572 PointsJavaScript Basics: Code Challenge - Stuck with using a conditional statement with a function
I'm finding learning functions a bit tricky and not being able to come up with an answer to the following code challenge:
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.
I'm unsure as to whether you can use return in conditional statements?
My code:
function max (number1,number2) {
if (number1>number2) {return number1};
}
else if (number2>number1) {return number2};
3 Answers
Pepe Suarez
18,267 PointsHey Will!! You are only missing the semicolons at the end of your return statements!
function max(num1,num2){
if (num1 > num2){
return num1;
}else if (num2 > num1){
return num2;
}
};
this is the way I did and it work
MOD changed from 'Comment' to 'Answer'
Will Peach
6,572 PointsThanks! Should have known it was something simple like that...
Matt Wolford
8,134 Pointsfunction max (2 , 1) { if (2 > 1) { return 2; } else { return 1; } }
what wrong with this its saying there was an error with your code: SyntaxError: Parse error, but i cant find the erro
Will Peach
6,572 PointsI think it might be to do with the assignment of your function where you are not setting 'parameters' but rather values (2, 1). You also need to remember to set the second half of the statement, where the if else retrieves the result of what happens in the case that 1>2.
It should look something like this:
function max(num1, num2){
if (num1 > num2){
return num1;
} else if (num2 > num1){
return num2;
}
};