I am trying to write a function to make the test case pass but when I tryna change value in my new arreay it make my default array change as well.
Here is my function:
function paintShop(cars, colour) {
let paintedCar = [];
for (var i = 0; i < cars.length; i++) {
if (cars[i]["colour"] === "Red") {
paintedCar.push(cars[i]);
paintedCar[0]["colour"] === "Pink";
}
}
console.log("colour", colour);
console.log("paintedCar`", paintedCar);
console.log("cars", cars);
}
paintShop(
[
{ make: "Ford", model: "Fiesta", colour: "Red" },
{ make: "Land Rover", model: "Defender", colour: "Muddy" },
{ make: "Toyota", model: "Prius", colour: "Silver" },
{ make: "Honda", model: "Civic", colour: "Yellow" },
],
"Pink"
);
module.exports = paintShop;
and here is my test cases:
var paintShop = require("./paint-cars");
test("Paint shop", function () {
var cars = [
{ make: "Ford", model: "Fiesta", colour: "Red" },
{ make: "Land Rover", model: "Defender", colour: "Muddy" },
{ make: "Toyota", model: "Prius", colour: "Silver" },
{ make: "Honda", model: "Civic", colour: "Yellow" },
];
var unpaintedCars = [
{ make: "Ford", model: "Fiesta", colour: "Red" },
{ make: "Land Rover", model: "Defender", colour: "Muddy" },
{ make: "Toyota", model: "Prius", colour: "Silver" },
{ make: "Honda", model: "Civic", colour: "Yellow" },
];
var repaintedCars = [
{ make: "Ford", model: "Fiesta", colour: "Pink" },
{ make: "Land Rover", model: "Defender", colour: "Muddy" },
{ make: "Toyota", model: "Prius", colour: "Silver" },
{ make: "Honda", model: "Civic", colour: "Yellow" },
];
var output = paintShop(cars, "Pink");
// expect(output).toEqual(repaintedCars);
expect(cars).toEqual(unpaintedCars);
});
Can anyone help me solve this problem????