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);

5
  • 2
    what get cars is supposed to return? One big string? Array of strings? Commented Feb 1, 2023 at 10:19
  • You should learn how to debug your code Commented Feb 1, 2023 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' } Commented Feb 1, 2023 at 10:26
  • Does return stop a loop? Commented Feb 1, 2023 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. Commented Feb 1, 2023 at 10:30

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, realized I made a few rookie errors! just gonna keep practicing
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}`;
 }

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.