0

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

3
  • Don't forget about indentation. It's important to make your code more readable: Communicate structure and intent. Commented Oct 10, 2018 at 16:35
  • push(data[I]) but change the loop to use data.length. Use Array.isArray for the other case (or use ...data for argument. Commented Oct 10, 2018 at 16:41
  • Could you explain a little bit about ..data? I know that when there's supposed to return the parameters of a function ,there's something to do with "..data" ,but I don't know what key words to search for. thanks Commented Oct 10, 2018 at 16:47

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the code,but the fact of the matter is ,there's no need to accumulate all the brands in one array,it's more like giving a brand and get its fellow competitors according to the number of parameters. I would like to make the result dependent on the parameters passed. something like the following : console.log(audi.addCars(ford)); //expected result: ['ford'] or console.log(audi.addCars(bmw,ford,fiat)) // expected result: ['bmw','ford',fiat']
awesome! runs like a well-oiled machine!
0

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

1 Comment

this returns the following: (3) [Cars, Cars, Cars] and (4)[Cars,Cars,Cars,Cars]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.