+ 1

JavaScript undefined issue, not sure why

I get the result: Your new car is a undefined undefined undefined in my console with JavaScript. Here is the code: function makeCar(){ var makes = ['chevy', 'vw', 'toyota', 'fiat']; var models = ['aveo', 'beetle', 'etios', 'uno']; var years = [2009, 1965, 2013, 1999]; var colors = ['red', 'blue', 'yellow', 'purple', 'grey']; var convertible = [true, false]; var rand1 = Math.floor(Math.random() * makes.length); var rand2 = Math.floor(Math.random() * models.length); var rand3 = Math.floor(Math.random() * years.length); var rand4 = Math.floor(Math.random() * colors.length); var rand5 = Math.floor(Math.random() * 5) + 1; var rand6 = Math.floor(Math.random() * 2); var car = { makes: makes[rand1], models: models[rand2], years: years[rand3], colors: colors[rand4], passengers: rand5, convertible: convertible[rand6], mileage: 0 }; return car; } function displayCar(car){ console.log("Your new car is a " + car.year + " " + car.make + " " + car.model); } var carToSell = makeCar(); displayCar(carToSell);

21st Nov 2017, 5:54 PM
horcrux88
horcrux88 - avatar
2 Antworten
+ 2
You're going to kick yourself for this. :) https://code.sololearn.com/WijsZ8Ienih2/#js CHANGE: console.log("Your new car is a " + car.year + " " + car.make + " " + car.model); TO: console.log("Your new car is a " + car.years + " " + car.makes + " " + car.models); ^You simply forgot the 's' at the end of your object variables. I'd change the car object to reflect it being singular instead of plural. However, what I posted will fix what your error. *edit* Was looking at it in the playground and just refreshed. John is spot on with what he said, just add the 's' at the end, or correct the object itself to reflect that it's singular instead of plural.
21st Nov 2017, 7:02 PM
AgentSmith
+ 1
You define car.years, car.makes, car.models, but attempt to output properties without the trailing s.
21st Nov 2017, 6:58 PM
John Wells
John Wells - avatar