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

Help debugging this script

I get the following error in my script: Uncaught SyntaxError: Unexpected end of input

var cars = [
  {model : 'ZZ', mpg : 29},
  {model : 'ZY', mpg : 26},
  {model : 'YY', mpg : 23},
  {model : 'YX', mpg : 20}
];

function modelPick(mpg) {
  for (var i = 0; cars[i] <= cars.length; i++) {
    if (mpg === cars[i].mpg) {
      var testCar = cars[i].model;
      var carMileage = cars[i].mpg;
      alert("The " + testCar + " has " + carMileage + " miles per gallon");
    }
}

modelPick(26);

2 Answers

The error is being thrown because one of your controls statements in your function is missing a closing } brace. Let me know if you need another hint, but that should help you sort it out.

The syntax error is happening because you're missing a closing curly brace in the modelPick function. However, just fixing that wasn't giving me the correct output either. I got it to work using this:

var cars = [
  {model : 'Tesla', mpg : 29},
  {model : 'Civic', mpg : 26},
  {model : 'Mustang', mpg : 23},
  {model : 'Jeep', mpg : 20}
];

function modelPick(mpg) {
  for (var i = 0; i <= cars.length; i++) {
    if (mpg === cars[i].mpg) {
      var testCar = cars[i].model;
      var carMileage = cars[i].mpg;
      console.log("The " + testCar + " has " + carMileage + " miles per gallon");
      break;
    }
  }
}

modelPick(23);

Edit - Whoops, Luke beat me to it.