Bruce B
Bruce B

Reputation: 39

Adding the function parameters to an empty array

I've got a function along with an instance made using "new" :

function Cars (model, color, year) {
this.model = model;
this.color = color;
this.year = year;
this.allCars = [];

}

var bmw = new Cars('z4', 'white', 2010),
    benz = new Cars('cl', 'black', 2011),
    ford = new Cars('mustang', 'red', 2015),
    audi = new Cars('s3', 'yellow', 2013),
    fiat= new Cars('fat boy', 'purple', 2020);


Cars.prototype.addCars = function (data) {
for(let i=0; i<3; i++){
   this.allCars.push(data);
  return this.allCars;  
}
}

console.log(benz.addCars(bmw,audi,fiat));
console.log(benz.addCars(ford));

I've tried to create a function named "addCars" such that whenever I assign an instance to it ,like benz.addCars(x1,x2,x3,...),I'd be able to get an array of the cars mentioned as the parameters.

for example, I would expect to get the following result when I call

console.log(benz.addCars(bmw,audi,fiat))
// expected result: ['bmw','audi',fiat']

and the following result for a single parameter instance:

console.log(benz.addCars(ford));
//expected result: ['ford']

I'm just wondering how I can get this array filled using the function addCard. cheers

Upvotes: 0

Views: 58

Answers (2)

Akrion
Akrion

Reputation: 18515

You could also consider using a class setup like this:

class Car {
  constructor(brand, model, color, year) {
    this._brand = brand
    this._model = model
    this._color = color
    this._year = year
  }
  get brand() {
    return this._brand
  }
}

class Cars {
  constructor(cars = []) {
    this._cars = cars
  }
  addCars(cars) {
    cars.forEach(c => this._cars.push(c))
  }
  getBrands() {
    return this._cars.map(x => x.brand)
  }
}

let cars = new Cars([
  new Car('BMW', 'z4', 'white', 2010),
  new Car('Mercedes', 'cl', 'black', 2011),
  new Car('Ford', 'mustang', 'red', 2015),
  new Car('Audi', 's3', 'yellow', 2013),
  new Car('Fiat', 'fat boy', 'purple', 2020)
])

console.log(cars.getBrands())

Where you could use ES6 getter/setters etc.

Here is another way to do this with your setup:

function Cars(brand, model, color, year) {
  this.brand = brand;
  this.model = model;
  this.color = color;
  this.year = year;
  this.allCars = [];

}

var bmw = new Cars('BMW', 'z4', 'white', 2010),
  benz = new Cars('Mercedes', 'cl', 'black', 2011),
  ford = new Cars('Ford', 'mustang', 'red', 2015),
  audi = new Cars('Audi', 's3', 'yellow', 2013),
  fiat = new Cars('Fiat', 'fat boy', 'purple', 2020);


Cars.prototype.addCars = function(data) {
  if (Array.isArray(data)) {
    for (let i = 0; i < data.length; i++) {
      this.allCars.push(data[i]);
    }
    return data.map(x => x.brand)
  } else {
    this.allCars.push(data);
    return data.brand
  }
}

console.log(benz.addCars([bmw, audi, fiat]));
console.log(benz.addCars([ford]));

Upvotes: 1

BharathwajRK
BharathwajRK

Reputation: 59

Please try the below method/ answer.

function Cars (model, color, year) {
this.model = model;
this.color = color;
this.year = year;
this.allCars = [];

}

var bmw = new Cars('z4', 'white', 2010),
   benz = new Cars('cl', 'black', 2011),
   ford = new Cars('mustang', 'red', 2015),
   audi = new Cars('s3', 'yellow', 2013),
   fiat= new Cars('fat boy', 'purple', 2020);


Cars.prototype.addCars = function (data) {
   for(var i = 0; i < arguments.length; i++) {
     this.allCars.push(arguments[i]);
   }
  return this.allCars;  
}

 console.log(benz.addCars(bmw,audi,fiat));
 console.log(benz.addCars(ford));

Upvotes: 0

Related Questions