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 trialDaniel Haasenritter
Courses Plus Student 3,463 PointsNot sure what I'm doing wrong
To me it looks like it should work, what did I miss?
function max (a, b) {
if (a > b) {
return a;
}
else {
return b;
}
}
alert(max(a,b));
Daniel Haasenritter
Courses Plus Student 3,463 PointsSo I need to add var to a and b?
function max (var a, var b) { if (var a > var b) { return var a; } else { return var b; } }
alert(max(var a, var b) );
Travis Anderson
5,658 PointsIf you are going to declare the variables for testing, it must be done outside of the function. But I reiterate, this is only necessary in testing the function. My mistake, I could have worded that a little bit better.
Remember, the var keyword is only used when declaring a variable for the first time in a new scope, and is not used when declaring parameters in a function. So if you were to test your function when submitting it, I would think the solution would look something like this.
function max (a, b) {
if (a > b) {
return a;
}
else {
return b;
}
}
var x = 10;
var y = 6;
alert(max(x,y));
In your first solution, you were calling the function with undeclared variables a and b. I have replaced the function call with declared variables x and y which will be used in the function in place of parameters a and b. Again, the solution would have worked without the alert since the challenge only requires a function declaration, but testing your code is a good habit to get into!
Edit: As Ivan Bagaric mentioned, and I didn't think of, you may also use numbers to call your function rather than declaring variables to use. The gist of my overly complicated comment is that in alert(max(a,b)), a and b have no value because they are not declared. You need to replace them with numbers or declared variables.
4 Answers
Larry Coonrod
5,041 PointsWhen you set the function up you told it to expect to inputs when called (a, b). Initially a and b are undefined. So when you call the max function, in this case you need to provide it actual data to compare. Keep in mind that functions can also call strings and other functions as well. It's a little confusing at first, but as you work your way through the course you'll get the hang of it. This is what the challenge is looking for:
function max (a, b) {
if (a > b) {
return a;
}
else {
return b;
}
}
alert(max(600, 900));
Test it with different value inputs for a and b
Ivan Bagaric
Courses Plus Student 12,356 Pointsalert(max(a,b));
use numbers instead of a and b
example
alert(max(5, 7)); alert(max(4, 1)); alert(max(55, 100));
Daniel Haasenritter
Courses Plus Student 3,463 PointsOkay, First, thanks for showing me what they are looking for. Felt like hitting my head against a wall. Second, why would I switch from (a, b) to numbers? At no point in the initial function did I use numbers. I believe that is my disconnect. Travis mentioned setting up variables, but that is not what you did here...which in fact worked, so what happened?
Larry Coonrod
5,041 PointsWith (a, b) you are telling the function to expect two input values when called. When the function runs it is going to compare two numbers and determine which is the larger. So you have to supply those two numbers for it to work.
You can think of it as declaring variables(a,b) and when you call the function you are assigning values to those variables. It doesn't really matter what you call the input variables at this point. You could put (chocolate, vanilla) and get the same result.
Later in the course you'll write programs that will supply the inputs without you having to manually enter them. This is just teaching you the basics of how functions work.
Travis Anderson
5,658 PointsTravis Anderson
5,658 PointsUnless you have
assigned to any values before calling the 'max' function in your alert method, then it is currently comparing two unassigned variables. When testing your solution, try assigning the variables a number, but the code challenge should not not require you to call the function, just declare it.