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

JavaScript

JavaScript question - Assigning numbers to a function's parameter, using a conditional

Here is the assignment I'm being asked in the JavaScripts Basics course: "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 two parameters to see which is the larger of the two."

My first problems is assigning numbers as arguments: Here is the code I've written thus far:

function max (little, big) { little = 10; big = 20; }

The error message I'm receiving is: Bummer! The max function isn't returning a number.

Thanks for any help you can provide. If I can get this first part I may still need help with the conditional, but right now I won't list the code I've written for that.

Jim Gravitt

7 Answers

function max (little, big) { 
little = 10; 
big = 20; 
}

You need to create two variables to hold some form of numbers

var x = 7;
var y = 10;

You then need to pass these variables to a function called max

function max(a, b) {} //this is the function that will take in two numbers

max(x, y); //this is the call to that function.

a conditional statement is an if/else statement so you just need to add that into the function

var x = 7;
var y = 10;

function max(a,b) {
if (a> b) {  //this checks to see if x is bigger
   return a;  //this returns x because it is bigger
} else {  //in all other cases we return y
   return b;
}
}

max(x, y)

When b is returned, it is returned if b is equal to or greater than a which covers all situations.

Thanks. based on your reply, here is the code I entered: function max(x,y) { var x = 7; var y = 10; if(x > y) {return x;} else {return y;} }

When I enter this code I receive the following error message:

"Bummer! There was an error with your code: SyntaxError: Parse error"

It seems to make no difference is I first list the variables or first define the function.

Removing the "if" statement also results in the same error message.

Please let me know if I have misunderstood something. Thanks for your help.

Jim Gravitt

function max(x,y) { 
var x = 7;
 var y = 10; 
if(x > y) {
return x;
} else {
return y;
} 
}

The problem with this is you are declaring the variables inside the function. They need to be declared outside of the function and then passed in as I showed in the final example.

var x = 7;
var y = 10;

function max(a,b) {
if (a> b) {  //this checks to see if x is bigger
   return a;  //this returns x because it is bigger
} else {  //in all other cases we return y
   return b;
}

max(x, y)

Here again, the variables are declared at the top. The function is called at the bottom with x and y being sent into the function. These variables are then assigned to the parameters a and b so a will be the same as x and b will be the same as y. Then the arguments are checked a>b return a or else return b.

Thanks again. Based on your reply, here is the code I have entered:

var x = 7; var y = 10; max (x,y) { if (x>y) {return x;} else {return y;} function max (x,y) {}}

Please note that I did not include "a" and "b" because I did not see where these variables were defined. Also, I added the function at the end because the instruction calls for a function to be defined.

I tried no {} brackets at the end, one closing } and also three brackets: {}} to close the initial bracket after the max (x,y) {.

I continue to receive the following error message: "Bummer! There was an error with your code: SyntaxError: Parse error"

Thanks fore any additional suggestions you can offer. I appreciate your help.

Jim Gravitt

I'm sorry, I screwed up when I typed it again, I left out the word function which is extremely important!

var x = 7; 
var y = 10; 

function max(num1, num2) {
 if (num1>num2) {
return num1;
} else {
return num2;
}
};

max (x,y);

when you make the function call you do not need the word function to make the call just the name ()'s and if their are arguments to pass in then those as well.

As for A/B it only matters that your parameter names are meaningful. So doesn't matter if it helps you think of them as X/Y all the way thru. A and B don't have to be defined using var though as they were placeholders. Meaning that something was being passed in and, they were being assigned something.

Also, you can assign the function call to a variable as well so you can do something with it.

var largerNumber = max(x, y);

Thanks Eric. That worked! I was also able to finish the second challenge so I'm on my way forward. Thanks again for your patience.

Jim Gravitt (Louisville, Ky)

P.s. Where do you live?

I'm in Anaheim, CA

westsiiiiide....... er rider....!