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
Hanan Al-Mashhadani
7,769 PointsI didn't understand it Can anyone explain it to me ??
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.
4 Answers
Grant G
19,934 PointsI believe, it should look like this:
//Create function named max
//Which accepts two arguments x and y
function max (x, y) {
//Then comparing them using if/else conditional statment
if (x>y) {
return x; //if x is larger than y, return x
} else {
return y; // else return y
}
}
max (5, 10); // should return 10
Hanan Al-Mashhadani
7,769 PointsThank you Grant Gasparyan I write it like this
function max (10, 5) { if (10 > 5) { return 10; } else { return 5; } }
but it's not true what is the wrong with it ??
Grant G
19,934 PointsFor your code interpreter returns "SyntaxError: Unexpected number." I think it is because, you shuld not use numbers as an arguments while creating function, arguments are variables inside the funtion, so they should be called like variables. You can pass the values, while function call.
if we replace the number attributes to x and y arguments
function max (x, y) { if (x > y) { return x; } else { return y; } }
and then call function passing values for arguments
max (5, 10)
it should work.
or You can call function right away, by wrapping function in brackets, like this
(function max (x, y) { if (x > y) { return x; } else { return y; } } ) (5, 10)
Hanan Al-Mashhadani
7,769 PointsIt's work :) Thank you so muck