1

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);
gog
  • 10,367
  • 2
  • 24
  • 38
  • 2
    what `get cars` is supposed to return? One big string? Array of strings? – gog Feb 01 '23 at 10:19
  • You should learn how to [debug your code](https://developer.chrome.com/docs/devtools/javascript/) – Liam Feb 01 '23 at 10:20
  • **Note 1** In addition to other answers and comments, the OP does not create a standard car object/item by the `addCar` method cause the object will be `{ carMake: 'Toyota', carModel: 'Corolla', carYear: '2003' }` instead of the expected standard model of `{ make: 'Toyota', model: 'Corolla', year: '2003' }` – Peter Seliger Feb 01 '23 at 10:26
  • [Does return stop a loop?](https://stackoverflow.com/q/11714503) – VLAZ Feb 01 '23 at 10:26
  • **Note 2** `carFactory` should 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 `_cars` property. – Peter Seliger Feb 01 '23 at 10:30

3 Answers3

3

You have a return in your for, which will exit the loop at the first iteration:

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}`;
 }
Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34
3

The issue with your code is that the return statement inside the for loop only returns the first car object in the _cars array and terminates the loop. To return all cars, you can concatenate the car objects into a string and return it after the loop:

const carFactory = {
    _cars:[
        {
            make: 'default',
            model: 'default',
            year: 0,
        }
    ],

    get cars(){
        if(this._cars.length > 0 ){
            let allCars = '';
            for(let i = 0; i < this._cars.length; i++){
                allCars += `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}\n`;
            }
            return allCars;
        }else{
            return `Please add car details`;
        }
    },

    addCar(carMake, carModel, carYear){
        this._cars.push({
            make: carMake,
            model: carModel,
            year: carYear
        })
    }
}

carFactory.addCar('Toyota', 'Corolla', 2003);

console.log(carFactory.cars);

Output:

Car Make: default - Car Model: default Manufacture Year: 0
Car Make: Toyota - Car Model: Corolla Manufacture Year: 2003
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
1

if you want an array in return then return an array. and addCar method need to have right name.

const carFactory = {
    _cars: [
        {
            make: 'default',
            model: 'default',
            year: 0,
        }
    ],

    get cars() {
        if (this._cars.length > 0) {
            let allCars = [];
            for (let i = 0; i < this._cars.length; i++) {
                let c = `Car Make: ${this._cars[i].make} - Car Model: ${this._cars[i].model} Manufacture Year: ${this._cars[i].year}`;
                allCars.push(c)
            }
            return allCars
        } else {
            return `Please add car details`;
        }
    },

    addCar(make, model, year) {
        this._cars.push({
            make,
            model,
            year
        })
    }
}

carFactory.addCar('Toyota', 'Corolla', 2003);
console.log(carFactory.cars);
Deep Saha
  • 11
  • 3