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 trialMathew Yangang
4,433 Pointsnot sure what i'm doing wrong here
Not sure what i'm doing wrong here
a=1;
b=2;
function max(a,b){
if(b>a);
return(2);
}
2 Answers
paulscanlon
Courses Plus Student 26,735 PointsHey
Almost
var a = 1;
var b = 2;
function max(a, b) {
if (a > b) {
return a;
} else {
return b
}
}
You return the variable a or b not the value of them
Hope this helps
Happy coding
Paul
Mathew Yangang
4,433 PointsThanks Paul
Christian Ali Nazha
Courses Plus Student 1,786 Points3 mistakes:
- wrong function syntax
- your logic works only if a number is bigger than the other, not the other way round
- always returning the same number instead of the input
Answer:
function max (a,b){ if(a>b){ return a; }else{ return b; } }
This function:
- Takes two numbers, the first input will be assigned to a while the second is assigned to b
- A condition checks if a>b, if so, a is returned
- An else catches the if, and will return b
Although this passes the requirements of your exercise, this has a glitch of always returning 'a' if 'a' and 'b' are equal.
Mathew Yangang
4,433 PointsMathew Yangang
4,433 PointsThanks Christian