General Question - Use class Syntax to Define a Constructor Function

General Question - Use class Syntax to Define a Constructor Function
0

#1

Tell us what’s happening:
Not a problem but a general question. Exercise URL is https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-class-syntax-to-define-a-constructor-function

Here is the code that passes

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

Seems like choices have been pre-determined as to what should be typed as solutions to these challenges.

My previous code for this challenge was this which didn’t past the tests.

  "use strict";
  /* Alter code below this line */

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

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

Would this have worked in an environment outside of this coding environment? This makes it difficult at times having to code exactly the words in the code that have been pre-set as the solution and makes things more confusing.


#2

Your code passes for me when I try it. Keeping in mind your pastes dont have the top of the code, where the full bit is:

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

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

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

You can name your arguments anything you want as long as you remember to make them unique in the scope.


#3

Just a few seconds ago I tried it again and it worked. I must have forgotten something previously but both ways work now.

Thanks!