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 trialRifqi Fahmi
23,164 Points[HELP] Challenge Task 1 of 2 javascript parameter
here is the task 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.
the hint say u need to use conditional statement. but i just type this code then i pass without typing conditional statement.
is my code right?? or are there any complete code to boost my comprehension!!
Thanks :)
function max(num1, num2) {
return num1, num2
}
8 Answers
Cena Mayo
55,236 PointsHi Rifqi,
Not quite. A conditional statement is one that checks to see if a statement is true or false (a boolean value). So you'd want to do something like this:
function max (a, b) {
if (a>b) {
return a;
} else {
return b;
}
}
This says that if a is greater than b, return a; otherwise return b. (Please note that it does not take into account numbers that are of equal value. Well, technically I suppose it would return b if that were the case, but that's likely to be a suboptimal result. :))
Best, Cena
Jack O'Brien
2,941 PointsAnybody else cruise through HTML and CSS so far and then encounter JS be like wtf lol. It is almost a new dialect, vastly more complex than HTML or CSS
Aron Jeremic
2,798 PointsI tried this code and works nicely, I passed the task.
function max(x, y) {
var bigger = Math.max(x, y);
return bigger;
}
Omar Ocampo
3,166 PointsI'm sorry, I'm stuck in this task, Does anybody have the correct answer?, please let me know
Thanks a lot guys
David Durant
4,387 PointsI think it is because you passed it numbers. When you make the Function think that the arguments() are like naming Variables can't start with a number. So, if you set (3,6) at the top the function will fail... but if you write it this way:
function max(x,y){ if( x > y){ return x; }else{ return y; } } max(6,3);
you are basically saying var x = 6 var y = 3
and the function max will run.
Brandon Perkins
1,499 PointsExact issue I was having. Super helpful. Thanks David!
Omar Ocampo
3,166 PointsThis code works perfect with letters but not with numbers, thanks
Cena Mayo
55,236 PointsHi Jon,
It's because you're attempting to pass arguments into your function definition, rather than parameters (which are placeholders for actual values, or arguments.)
For the above to work, it would need to look like this:
function max (a, b) {
if (a > b) {
return a;
} else {
return b;
}
}
then call the function with:
max(3,6);
jonschafer
2,229 PointsAhhhhh! Thank you so much!
Jason Headley
2,884 Pointsfunction max(x,y){
if ( x > y){
return x;
} else {
return y;
}
}
//this works
Gertrude Dawson
5,371 PointsThank you all. I now understand the theory behind the answers.
jonschafer
2,229 PointsSo, why doesn't this work?
function max (3, 6) {
if (6 > 3) {
return 6;
} else {
return 3;
}
}
Olu Aganju
Courses Plus Student 731 Pointsyou got it , just change your 3 & 6. so function max (6,3) { if (6 > 3) { return 6; } else { return 3; } } (the problem is your original placement of (3,6) switch it to (6,3) and it works! ) Think of it as a & b , If a is greater than be then so on. Remember the original placement is (a, b) .
Linus Karlsson
7,402 PointsLinus Karlsson
7,402 PointsI understand everything but one thing in your explanation: where are the arguments? Shouldn't you call them at the end?