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

I am at such a loss with this challenge can someone help explain it to me? NEWBIE HERE

Thanks guys

script.js
var number1 = 1
var number2 = 9

function max(1, 9){
  if(1>9){
    return number1;  }
  else{return number2;}  
alert (max);
}

The function definition should contain variable names, not literal values. Instead of max(1, 9), do max(number1, number2) (and replace the 1 and 9 with number1 and number2 inside the function).

3 Answers

jason chan
jason chan
31,009 Points
//first challenge
function max (x, y) {
  if (x > y){
  return x;
  } else if ( y > x) {
  return y;
  }

}
//2nd challenge
var hey = max(2,3);
alert(Math.random(hey));

create a function named max pass in x or y (numbers) if x is greater than y return x, else if y > x then return y.

define variable hey = max(2, 3); // call two numbers alert(Max.random(hey)); // alert max random number

jason chan
jason chan
31,009 Points

it's else if code block. It's not if-else.

If true

do this

else not true

do this

but this is either or so. If this is true return this, else if this is true return this.

Chase Swanson
PLUS
Chase Swanson
Courses Plus Student 8,886 Points

You are getting close.

First, you need to pass in the variables number1 and number2 to the function. Then your if statement should compare number1 to number2 and return the appropriate variable for whichever is greater.

Does that make sense?

Also, you need to move the alert statement outside the function block. Right now you are trying to call a function inside its own definition.