I'm new to JavaScript and working on a personal program which creates car objects and stores them in an array, I am having issues with returning all array elements as only the first array element is returned.
const carFactory = {
_cars:[
{
make: 'default',
model: 'default',
year: 0,
}
],
get cars(){
if(this._cars.length > 0 ){
for(let i = 0; i < this._cars.length; i++){
return `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}`;
}
}else{
return `Please add car details`;
}
},
addCar(carMake, carModel, carYear){
this._cars.push({
carMake,
carModel,
carYear
})
}
}
carFactory.addCar('Toyota', 'Corolla', 2003);
console.log(carFactory.cars);
get carsis supposed to return? One big string? Array of strings?addCarmethod cause the object will be{ carMake: 'Toyota', carModel: 'Corolla', carYear: '2003' }instead of the expected standard model of{ make: 'Toyota', model: 'Corolla', year: '2003' }carFactoryshould be renamed to something else since it is not a factory function but an object which serves as namespace for methods and a pseudo-private_carsproperty.