JavaScript JavaScript Basics Creating Reusable Code with Functions Create a max() Function

Ayrton Tavares
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

Nick Trabue
PRO
Nick Trabue
Pro Student 12,651 Points

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.