Use class Syntax to Define a Constructor Function_does not pass the test

Tell us what’s happening:
I’m using the right solution from “HINT” but my code is not passing the test.:

// running tests

carrot.name should return carrot.

// tests completed

Your code so far


function makeClass() {
"use strict";
/* Alter code below this line */

class Vegetable {
constructor(Vegetable) {
this. Vegetable = 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 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

You’re right, the solution is wrong. We should make a PR to fix it.

You can fix it by replacing this.Vegetable with this.name.

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

But even better would be to replace everything with name, it makes more sense:

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

Could you by any chance explain to me like I’m 5 what exactly happens in this example (when “name” in constructor and in the next line)?

@ghukar,
this.name = name will not pass the last test…

The hint code

  "use strict";
  /* Alter code below this line */
class Vegetable {
     constructor(Vegetable){
       this.Vegitable = Vegetable;
  
     }
   }

  /* Alter code above this line */
  return Vegetable;
  /* Alter code above this line */
  return Vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name); // => should be 'carrot'

doesn’t work because this is referring to the const carrot that creates a new Vegetable.
So using the code above if you console.log(carrot.Vegetable); logs “carrot”.

You could use any name you want after this, it could be “rasin”. Then change the log to console.log(carrot.rasin) and you will see it logs “carrot”.

I hope this helps you see what is happening in the code. But the tests are looking at console.log(carrot.name); so that is why you have to use this.name.