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

Cordez Coleman
PLUS
Cordez Coleman
Courses Plus Student 1,231 Points

I keep getting an error

It seems like i'm a slow learner, i've written this code a few different times and I feel as if i'm just making it worse.

script.js
function max(car, tree) {
 var car = 20;
  var tree = 10;

  if (car > tree)}
    return car;
else {
  return tree;
}

2 Answers

Leonardo Motta
Leonardo Motta
11,284 Points

All opening curly brace should have a closing curly brace.

function() {if {} else {}}

function() {
  if {

  } else {

  }
}
function max(car, tree) {
  var car = 20;
  var tree = 10;

  if (car > tree) {
    return car;
  } else {
    return tree;
  }
}
Cordez Coleman
Cordez Coleman
Courses Plus Student 1,231 Points

Thanks for someone reason I kept thinking the return was on the outside of the {}

Leonardo Motta
Leonardo Motta
11,284 Points

Well, I don't know if you are coding mainly on workspaces, it is good to follow the classes, but try coding in vscode/atom/brackets... a more modern text editor, they are a lot helpful with this kind of detail and also highlighting curly braces and some other details of the language syntax

Matthew Long
Matthew Long
28,407 Points

The main issue is your curly braces aren't correct. You want them to look like the following:

if () {

} else {

}

The other issue is you're setting the parameters equal to something inside the function. This kind of defeats the purpose for this situation so remove the following lines:

var car = 20;
var tree = 10;

Simply giving you the answer to copy and paste isn't fair to your studies so I'll let you take it from here!