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

Sandra Mazzocato
seal-mask
.a{fill-rule:evenodd;}techdegree
Sandra Mazzocato
Front End Web Development Techdegree Student 8,260 Points

can´t complete this task

Hi

I would like to know the answer to this task to know where I was wrong.

Thank you.

script.js
function max (1, 2){
  if (1 > 2){
  var maior = 1;
  }
  else{
  var maior = 2;}
 return maior;
}

3 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

Your parameters are numbers, so when you are saying if (1 > 2) {} it will always be false because 1 is smaller than 2. You cant use numbers for variable names for this reason.

This is what you are looking for:

function max (x, y){
  if (x > y){
    return x;
  }
  else{
    return y;
  }
}
Andrew Kiernan
Andrew Kiernan
26,892 Points

Hi Sandra,

Numbers are not valid variable names, and cannot be used as function parameters. You can however, use the word for each number as a variable name:

function max(one, two) {}

If you replace the 1 and 2 with one and two, it should pass. However you can also simplify this a bit and remove the maior variable. In your if/else blocks, you can just return the parameter itself (e.g. return one or return two).

I hope that helps!

-Andrew