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 trialMUZ140884 Maxwell Makumucha
6,291 PointsHow to create a new function named max() which accepts two numbers as arguments using conditional statements
help guys
function max(20,10){
if(20>10){
return 20;}
}
1 Answer
Ted Sumner
Courses Plus Student 17,967 PointsThere are several issues with your code. First, you are not passing numbers to your function. You should replace the numbers with variables. Second, once you replace the numbers with variables, you are only returning one if it is larger. You need an if (one is larger) {return that} else {return the other number}. This is what I came up with that passes:
function max(x, y) {
if (x > y) {
return x;
} else {
return y;
}
}
MUZ140884 Maxwell Makumucha
6,291 PointsMUZ140884 Maxwell Makumucha
6,291 Pointshie , im trying to write the code u gave me but is saying : Bummer! There was an error with your code: SyntaxError: Parse error. i don't know what to do next can you help Sir
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsPlease post what you are trying.
MUZ140884 Maxwell Makumucha
6,291 PointsMUZ140884 Maxwell Makumucha
6,291 Pointsthis is the code : function max(20, 10) { if (20 > 10) { return 20; } else { return 10; } }
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsYou are still using numbers. The challenge requires using variables like I did above.
You generally do not use numbers like that in programming. Variables are better because you can set them programmically and have a code that can be used for different purposes. Furthermore, the
function max(20, 10)
expects variables to be inside the parenthesis. You cannot place numbers there.You said that you are trying the code that I gave you, but you posted your old code. Try the code I gave you exactly.