Necesito ayuda con un problema en el que estoy trabajando. Aquí están las instrucciones:
Escribe una función llamada fábricadeautos que tome tres parámetros: marca, modelo y año.
Cuando se invoca la función:
Dentro de la función, cree un objeto a partir de esos parámetros. Luego, escriba una declaración if que verificará si el año enviado es posterior a 2018.
Por último, la función debe devolver el objeto. Por ejemplo:
carFactory('toyota', 'camry', 2020) // should return an object that looks like this: { make: 'toyota', model: 'camry', year: 2020, isNew: true };Esto es lo que tengo hasta ahora, pero no está produciendo los resultados deseados. Cualquier ayuda sería muy apreciada.
function carFactory(make, model, year) { this.make = 'make'; this.model = 'model'; this.year = 'year'; let newCar = { make: this.make, model: this.model, year: this.year, } if (year > 2018) { carFactory.isNew = true; } else { carFactory.isNew = false; } }; let newCar = new carFactory('toyota', 'camry', 2020); console.log(newCar);Puede crear una clase de automóvil y devolver la nueva instancia de automóvil de fábrica. Esto le dará el resultado esperado
class Car { constructor(make, model, year) { Object.assign(this, { make, model, year }); } } const carFactory = (make, model, year) => { const newCar = new Car(make, model, year); newCar.isNew = (year > 2018); return newCar; }; const newCar = carFactory('toyota', 'camry', 2020); console.log({ newCar });Prueba así:
function carFactory(make, model, year) { this.make = make; this.model = model; this.year = year; year > 2018 ? this.isNew = true : this.isNew = false }; let newCar = new carFactory('toyota', 'camry', 2020); console.log(newCar);