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
Willie Suggs
Courses Plus Student 5,879 Pointshelp me figure this out without giving me the answer pls
Beneath the max function you just created, call it with two numbers and display the results in an alert dialog. Pass the result of the function to the alert method.
For example, to display the results of the Math.random() method in an alert dialog you could type this:
function max(num1, num2) { if( num1 < num2 ) { return num2; } } now i have to call the max faction with two numbers an show results in alert dialog box
max(num1, num2) { alert( max(num1, num2) ); }
2 Answers
Erik Nuber
20,629 Pointsfunction max(num1, num2) {
if( num1 < num2 ) {
return num2;
}
}
max(num1, num2) { alert( max(num1, num2) ); }
In your function, you have an if statement that takes care of one situation but, not the other. So what if num1 > num2
Also, you need to assign numbers to your function call. The way you have your function call set up, you will be calling it twice but, the second way is incorrect.
I am happy to help you if you need more but, I'm not sure how much further to help as anything more would be giving you the answer.
Michael Liendo
15,326 PointsA couple of things to help get you on the right track:
Your max function, as it stands now, only returns a value if the second number is bigger than the first. You should write another condition if the first number is bigger, and another if the numbers are the same.
Also, in JS when you call a function with a return value, all it's going to give back is that value. So calling
max(num1, num2) { alert( max(num1, num2) ); }
isn't correct syntax...I see your logic, but JS is different.
To help, but not give the answer away, if I had a function that adds to numbers together and wanted to call it, I would write:
addNumbers(2,3);
Obviously, it gives back 5. So if I wanted to log that out to the console, I would simply write:
console.log(addNumbers(2,3));
Hope that helps :)
Willie Suggs
Courses Plus Student 5,879 Pointsthanks!!! function max(num1, num2) { if(num1 < num2) { return num2; } } num1 = 1; num2 = 2;
alert( max(num1, num2) );
Willie Suggs
Courses Plus Student 5,879 PointsWillie Suggs
Courses Plus Student 5,879 Pointsthanks!!! function max(num1, num2) { if(num1 < num2) { return num2; } } num1 = 1; num2 = 2;
alert( max(num1, num2) );