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

Ayrton Tavares
seal-mask
.a{fill-rule:evenodd;}techdegree
Ayrton Tavares
Full Stack JavaScript Techdegree Student 898 Points

Need help - SyntaxError: Parse error

really lost with returning a specific value of an argument using a conditional statement.

script.js
function max (1,11) {
var largerNumber = 11;
var smallerNumber = 1;

  if (parseInt(largerNumber) === 11) {
    return largerNumber;
  } else {
        return smallerNumber;
  }
}

1 Answer

When you're defining a function, you don't want to include the variables you are testing. The key is to make the function repeatable so that you can use it throughout your code. Think of the parameters a function takes as two variables that have not yet been defined.

function max(a, b) {
    if (parseInt(a) > parseInt(b)) { 
    return a
  } else {
    return b
  }
}

Now you'll be able to call that function elsewhere in your code knowing exactly what your outcome will be

var numberOne = 12
var numberTwo = 4
max(numberOne, numberTwo) // will return 12
max(numberTwo, numberOne) // will still return 12

As such, It's probably not a good idea to declare a larger number and a smaller number as a variable within that function because the function is pure and does not care which order you pass the numbers.