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
Vanessa Elsayed
3,486 PointsAnother JavaScript Challenge Issue
I can't figure out the code challenge to this. Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.
5 Answers
rydavim
18,814 PointsBased on your description of the challenge, you'll want to create a function that can take two arguments and then use if-else structures to evaluate which number is bigger and return that number.
function max(numA, numB) { // The function needs to accept two numbers.
if (numA > numB) { // If numA is greater...
return numA;
} else if (numB > numA) { // Otherwise if numB is greater...
return numB;
}
}
Edit - Whoops! Sorry about that, there was a typo in my code. Fixed.
Nathan McRae
7,852 PointsI was also having trouble with this challenge. Thanks!
John Mann
1,711 PointsYou are missing an equal case and you can do in a simpler way.
The ? operator checks the previous expression (a >b) and the first value is returned if true, otherwise the second value.
function max(a, b) {
if (a == b) {
return 'equal';
}
return a > b? a : b;
}
John Lukacs
26,806 PointsIt works but I don't understand what all this is.
return a > b? a : b;
John Mann
1,711 PointsInside a function "return" returns a value. a > b is the condition I am checking for
the ? is the ternary (conditional operator) in JavaScript So the syntax for the statement is this: condition ? expr1 : expr2
Where expr1 gets returned if the condition is true and expr2 gets returned if the condition is false.
An equivalent (and probably more readable version is this:
if (a > b){
return a;
}else{
return b;
}
I just tried to make it concise as possible. Hope that helps.
John Lukacs
26,806 PointsOk my mind is blown is your mind blown but I tried it the simple way and it still dosent work. What is a ternary
John Mann
1,711 PointsTernary (by definition) means composed of three parts.
So the "?" is called a ternary operator, since it has the following:
conditional ? expr1 : expr2
function max(a,b){
if (a == b){
return 'equal';
}
if (a > b){
return a;
}else{
return b;
}
}
alert(max(3,4)); //Returns 4
alert(max(4,3)); //Returns 4
alert(max(4,4)); //Returns equal
Don't forget the closing {}. Hope that helps.
Vanessa Elsayed
3,486 PointsVanessa Elsayed
3,486 PointsUnfortunately I am getting this when i put it this answer, which is what close to what i had before i asked for help. Bummer! There was an error with your code: SyntaxError: Parse error
rydavim
18,814 Pointsrydavim
18,814 PointsSorry about that! There was a typo in my code. I've fixed it and tested it on the challenge this time. You should be good to go!
Vanessa Elsayed
3,486 PointsVanessa Elsayed
3,486 PointsThank you!!!!!
Abe Adoyta
4,920 PointsAbe Adoyta
4,920 PointsStill doesnt work for me :S