[Some bug ] Use class Syntax to Define a Constructor Function

Tell us what’s happening:
Same issue as this post. [Some bug] Use getters and setters to Control Access to an Object

I only used the class keyword. Didn’t setup the constructor method at all.

It still passed the test.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Vegetable {
    
  }
  /* Alter code above this line */
  return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function/

Thank you so much for reporting issues. Hopefully this one easier to fix.

This is great people help people, just like you reported the issues. Really appreciate it.

It’s because the test cases are not written for the functionality of the program.

FYI the following two videos helped me learn constructors/this exercise:
A. https://www.youtube.com/watch?v=F3GeM_KrGjI
B. https://www.youtube.com/watch?v=f5wGZiYVfjk

1 Like

I am still facing problems with the code. Please fix it.

What problems are you encountering? And what does your code look like?

And as an aside, unless your issue is related to this particular bug, it may make more sense to create a new thread for your issue.

class Vegetable {
constructor(Vegetable) {
this.Vegetable = Vegetable;
}
}

This is the code I have changed. It manages to pass all the other tests except
**this.carrot = carrot

where am I going wrong?

but it shouldn’t be this.carrot. It should be carrot.name.

You’re setting the property Vegetable to the value of the passed param, but that isn’t what the instructions say.

The Vegetable lets you create a vegetable object, with a property name , to be passed to constructor.

Thus, you’re shouldn’t be setting a property .Vegetable, you’re setting a property .name.

got it, Thanks!

My mistake: constructor(name) not constructor(Vegetable)

1 Like

Well, and inside the constructor,

 this.name = name;

Okay, so here’s my code:

  constructor(name){
    this.name = name;
  }

And I am getting the error, “unexpected token”. Does anyone have any idea what’s going on?

Please paste your code in ``` tags like so:

```
put your code here ...
```

It’s much easier to read than a screenshot. In this case, I can see the problem though: you need to define the constructor inside of a class, not the function.

And yes, it’s a little odd to see class used inside a function, but it does work (and for good reason)

Okay, it passed! Thank you so much! Sorry about the image.