Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Getting Cannot set properties of undefined

I have function CarFactory, which produce cars and should change color of these cars.

const warehouse = require("./warehouse");

function CarFactory(power = 10) {
  this.warehouse = warehouse;
  this.produceCar = (color = "red", wheels = 4, engine = false) => {
    if (power < 2) {
      return null
    } else {
      let car = { "id": warehouse.nextIdentifier, "color": color, "wheels": wheels, "engine": engine }
      warehouse.createdCars.push(car);
      warehouse.nextIdentifier++;
    }
  }

  this.addEnergyPower = (value = 0) => {
    power += value;
  }

  this.changeCarColor = (num) => {
    if (power < 1) {
      return null
    } else {
      warehouse.createdCars[num].color = 'blue'
    }
  }

}

module.exports = CarFactory;

But Im getting error Cannot set properties of undefined(setting 'color'). If I hardcode 0 like this to: createdCars[0] it actually works for car indexed 0.

this is warehouse file

let warehouse = {
  createdCars: [],
  nextIdentifier: 0
};

module.exports = warehouse;

this is where jest tries to change color

for (let i = 0; i < myFactory.warehouse.createdCars.length; i += 2) {
    let car = myFactory.warehouse.createdCars[i];
    if (myFactory.changeCarColor(car) !== null) {} else {
      if (energyBoosts.length > 0) {
        myFactory.addEnergyPower(energyBoosts.shift());
        i -= 2;
      } else {
        break
      }
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From what I can tell in the line if (myFactory.changeCarColor(car) !== null) {} else{ car would be { "id": warehouse.nextIdentifier, "color": color, "wheels": wheels, "engine": engine } but what it seems like you would want is the id. So change if (myFactory.changeCarColor(car) !== null) {} else{ to if (myFactory.changeCarColor(car.id) !== null) {} else{ and see if that works.

about 4 years ago · Juan Pablo Isaza Report

0

You have this.changeCarColor = (num) but appears you want to pass a car to that and then get the id? I have no idea what this is so I then fails here: console.log(typeof energyBoosts); with "undefined" for energyBoosts But that is the NEXT question not the one at hand.

let warehouse = {
  createdCars: [],
  nextIdentifier: 0
};

function CarFactory(power = 10) {
  this.warehouse = warehouse;
  this.produceCar = (color = "red", wheels = 4, engine = false) => {
    // console.log("here");
    if (power < 2) {
      return null;
    } else {
      let car = {
        "id": warehouse.nextIdentifier,
        "color": color,
        "wheels": wheels,
        "engine": engine
      };
      warehouse.createdCars.push(car);
      warehouse.nextIdentifier++;
    }
  }

  this.addEnergyPower = (value = 0) => {
    power += value;
  }

  this.changeCarColor = (car) => {
    console.log(car.color, car.id, power);
    if (power < 1) {
      return null;
    } else {
      warehouse.createdCars[car.id].color = 'blue';
    }
  }
}

let myFactory = new CarFactory();
//console.log(myFactory);

for (let c = 0; c < 10; c++) {
  //console.log(c);
  myFactory.produceCar("red", 4, false);
}

//console.log("GO!", myFactory.warehouse);
for (let i = 1; i < myFactory.warehouse.createdCars.length; i += 2) {
  //console.log(i);
  let car = myFactory.warehouse.createdCars[i];
  console.log("Car:", car);
  if (myFactory.changeCarColor(car) == null) {
    console.log(typeof energyBoosts);
    if (energyBoosts.length > 0) {
      myFactory.addEnergyPower(energyBoosts.shift());
      i -= 2;
    } else {
      break;
    }
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!