DEV Community

CodeMonk
CodeMonk

Posted on

Here is how ChatGPT helped me to fix my piece of code.

hey correct my javascript code which i need in es6 version

Of course! I'll be happy to help you convert your JavaScript code to the ES6 version. Please provide me with the code you'd like me to assist you with.

'use strict';

class Car {
  constructor(make, speed) {
    this.make = make;
    this.speed = speed;
  }

  accelerate() {
    this.speed += 10;
    console.log(`${this.make} is going at ${this.speed} km/h`);
  }
  brake() {
    this.speed -= 5;
    console.log(`${this.make} is going at ${this.speed} km/h`);
  }
  car1 = Car('BMW', 120);
  car2 = Car('Mercedes', 95);
}
accelerate();
Enter fullscreen mode Exit fullscreen mode

Sure! I have made some corrections to your code to align it with ES6 syntax. Here's the corrected code:

'use strict';

class Car {
  constructor(make, speed) {
    this.make = make;
    this.speed = speed;
  }

  accelerate() {
    this.speed += 10;
    console.log(`${this.make} is going at ${this.speed} km/h`);
  }

  brake() {
    this.speed -= 5;
    console.log(`${this.make} is going at ${this.speed} km/h`);
  }
}

const car1 = new Car('BMW', 120);
const car2 = new Car('Mercedes', 95);

car1.accelerate();
Enter fullscreen mode Exit fullscreen mode

Here's what has changed:

  1. Removed the car1 and car2 assignments from within the class definition as they should be separate instances outside the class.
  2. Created car1 and car2 instances using the new keyword and the Car constructor.
  3. Called the accelerate method on the car1 instance outside the class.

Now, you can run the code and it should work as expected. Let me know if you need any further assistance!

Top comments (2)

Collapse
 
monkcodes profile image
CodeMonk
Collapse
 
lidiaaadotnet profile image
lidiaaadotnet

Simple but concise, that's how content should be!

By the way, you can add js in front of the first code excerpt so that the syntax highlighting is displayed.

ps: it won't let me access the ChatGPT chat link 🥺

And of course, welcome to Dev.to! I hope you feel comfortable in this great devs community!😊