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 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;
}