Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

mustafa attaie
mustafa attaie
8,068 Points

Stuck with conditional statements inside a function with numbers challenge task 1 of 2

can't seem to quite understand what I'm doing wrong here? Maybe I'm not understanding the way they're phrasing the question. Can someone explain to me what I'm doing wrong please?

script.js
function max(2, 9) {
  if(9 > 2) {
    return 9;
  } else {
    return 2;
  }

3 Answers

Karolin Rafalski
Karolin Rafalski
11,368 Points

Variable names cannot be numbers. You need to change 9 and 2 to valid variable names, for example: you could use num1, num2 instead.

mustafa attaie
mustafa attaie
8,068 Points

So what ever you add as the arguments become a variable?

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

There are two problems here. Number one, you're missing the ending closed curly brace for your function. Number two, your code must be more generic. You are hard-coding the values.

:information_source: The guys and gals at Treehouse are going to send (possibly multiple times) different numbers to your function to make sure that what you return is what it should return. But we have NO idea which numbers they're going to send. So we use variables to (aka parameters) to sort of "catch" whatever they're sending. Take a look:

function max(a, b) {
  if(a > b) {
    return a;
  } else {
    return b;
  }
}
Karolin Rafalski
Karolin Rafalski
11,368 Points

Yes, when you create a function, whatever you set as the parameters are automatically declared as variables inside that function.

like so:

function myNewFunction (parameter1, parameter2) {
return parameter1 +parameter2;
}