Argument didn't work

class vehicals{


    constructor(color, model){
        this.color = color
        this.model = model
    }


}

function changeColor(car, Newcolor){
    car.Newcolor = Newcolor

}

let car1 = new vehicals("Corvette", "blue")
let car2 = new vehicals("Mustang", "red")


changeColor(car1, "purple")

console.log(car1.color, car1.model)
console.log(car2.color, car2.model)

“Car1” color didn’t change to purple.

car.NewColor doesn’t exist.

Lint tools can assist in identifying these types of simple mistakes and typos.

Oh, that’s why. I have to pay more attention.

^ what windbeneathmywings said.

But there’s also quite a lot that could be improved in the code.

For example, the class name should have a capital and be in the singular.

Your constructor looks like this:

constructor(color, model){

But you are calling it like this (arguments are in the wrong order):

let car1 = new vehicals("Corvette", "blue")

And it would be better to make the changeColor function a method on the class:

class Vehicle {
  constructor(model, color) {
    this.model = model;
    this.color = color;
  }

  changeColor(newColor) {
    this.color = newColor;
  }
}


const car1 = new Vehicle('Corvette', 'blue');
console.log(car1.color);

car1.changeColor('pink');
console.log(car1.color);

Also, you could use const for things you won’t be reassigning (as opposed to let).

1 Like

I see, thanks

You have multiple errors in your code.

First of all, changeColor() should be a method of the class vehicle and not an extern function.
Second, why should car.Newcolor = newColor change the color of the car?
Third, take care of the camelCase. Variable names always start with a lowercase. So it should be newColor.
The class contains only one vehicle and class names start with an uppercase. So it should be class Vehicle.
There are many semicolons missing.

Here is a working fiddle

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.