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

creating a max function

"Beneath the max function you just created, call it with two numbers and display the results in an alert dialog. Pass the result of the function to the alert method."

I am confused a bit here. I have been stuck at this problem for over a week now and I still can't seem to figure it out. What is the problem? it says "oops! It looks like task 1 is no longer passing." but I have not changed any of the code I typed in task one.

script.js
function max (one, six) {
 if ( one > six ) {
  return one;
 } else {
   return six;
 }
}

alert( max(one, six) );

2 Answers

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

Hi there! The second step asks you to pass in two numbers , but you are passing in two undefined variables. This is causing a syntax error which means that the code can no longer be interpreted.

The parameters in the function definition one and six will be assigned any values we send in when we call the function. So if I were to do this:

alert( max(20, 3) );

I would then pass in 20 which would be assigned to the variable one and 3 which would be assigned to the value six. I feel like you might have confused yourself here a bit by making parameter names the name of a number. I might have chosen the names num1 and num2.

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

It will take the first number we send in and assign the value to num1 and the second number to num2.

Hope this helps! :sparkles:

Why does this pass!?

function max(firstNumber, secondNumber) {
    if (firstNumber > secondNumber) { 
        return firstNumber; 
    } else { 
        return secondNumber;
    }
}

alert( Math.random( max ) );
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

ilya A because the first part of the code is absolutely correct. Although you have the alert in there, I believe Treehouse is ignoring that. It's Treehouse that's running that function... not you. When they run the function they will send in two numbers of their choosing and we have no idea what those are, so the code must work for any two numbers sent in.

oh okay I see, so when I call the function it ask me for the replacements/input/argument? when I make the function I make the parameters/question?

Thank you Jennifer, you were of great help!