Use class Syntax to Define a Constructor Function

Use class Syntax to Define a Constructor Function
0

#1

Where is error ? :confused:

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Vegetable {
    constructor(vegetable){
      return this.vegetable;
    }
  }
  /* Alter code above this line */
}
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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 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


#2

You need to pass in the vegetable name. The constructor is used to set up the object. I shouldn’t actually return anything. You need to change this.vegatable to this.name as the test is looking for a name property to be set:

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

#3

thank you so much…:+1:t2:


#4

I’ve passed the challenge with this code :man_shrugging:t2: :slight_smile:

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

#5

Hi, try to open up the console and execute your code, you will see the result of
console.log(carrot.name) will be undefined :slight_smile:


#6

Right, but that code is indeed passing the tests for the challenge, while that code should be failing. That should be filed as a bug @collinstommy, if it hasn’t already.


#7

I’d say there is a bug in this assignment somewhere… I passed the assignment with this

 "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'```

#8

Hi there! This is my first contribution to the community. I hope that my manners are proper ennough: I don’t live in a english speaking country.

So my question is…Why do we need wrapping the constructor inside a makeClass function and assignning it to the Vegetable variable? I’ve checked myself passing the test without this (just by removing the wrapping function and removing the assignment) and , not very surprisingly, tests went green.

Tx in advance!