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 trialDaniele Manca
10,986 PointsUnable to complete this task
I am trying with the below code:
function max( 1, 2 ) { if ( 1 < 2 ) { return 2; } else { return 1; } }
function max( 1, 2 ) {
if ( 1 < 2 ) {
return 2;
} else {
return 1;
}
}
4 Answers
Michael Hulet
47,913 PointsAlthough I'm not sure what the task does, I bet I can guess what's going wrong. With your code, JavaScript will evaluate the numbers literally, instead of as variable. In other words, you can't use just numbers to name variables in JavaScript. Try this code:
function max(first, second){
if(first < second){
return second;
}
else{
return first;
}
}
Ayoub AIT IKEN
12,314 Pointsfunction max( a, b ) {
if ( a < b ) {
return b;
} else {
return a;
}
}
huckleberry
14,636 PointsYou're using numbers within the parameters and you're most likely getting a syntax error. Avoid using numbers as your parameters.
Here's another version that will always return the bigger of the two numbers.
//Function set to always return the biggest number of a pair
function max(num1,num2){
var bigger;
if (num1 > num2){
bigger = num1;
return bigger;
}
else {
bigger = num2;
return bigger;
}
}
//Calling the function and displaying it with an alert
alert(max(13,45));
Daniele Manca
10,986 PointsThanks folks, :)