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'm told there is a syntax error on line 4. I thought this was how a conditional statement was set up.

.

script.js
function max(first, second) {
    var first = 3;
  var second = 5;
  if (var first > var second){
    return var first;
  }  else (var second > var first)
    return var second;
    }
}

1 Answer

Dario Bahena
Dario Bahena
10,697 Points

The issue is with the keyword var. var is used to declare a variable. Once it is declared, it should not be used again for that specific variable. This will get rid of the syntax errors. There is also some logic error. Your function is taking two parameters (first and second) but you are then re-declaring those variables and setting their values. When you put variables in your parameters, JavaScript automatically interprets them as var so there is no need to declare them.

function max(first, second) {
// the variables "first" and "second" have already been declared. No need to write "var first" anymore.
  if (first > second){
    return first;
  } else if (second > first){ // you were also missing this brace and if condition
    return second;
    }
}
// now you can use your function with any number

console.log(max(10, 93)); // prints 93 to the console.