Getting Reference Error for the challenge Use Class Syntax to Define a Constructor Function

Tell us what’s happening:
The solution code is not working. The error thrown is Reference Error, I am guessing something is wrong with the function that’s been called.

Your code so far

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

const temp = makeClass();
const veggies = new Vegetable('brocolli');
console.log(veggies.name);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0.

Challenge: Use class Syntax to Define a Constructor Function

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

FYI I did not copy-paste it. On top of that, the question was meant for general sense not just for this code challenge. The solution that I wrote does not even include function. This is the solution I have in my mind:-

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

const carrot= new Vegetable('carrot');
console.log(carrot.name);

Here is another example that I wrote one that is a bit more challenging:-

class BuyPhoneForDad {
        constructor(phoneModel, price, gb){
            this.phoneModel = phoneModel,
            this.price = price,
            this.gb = gb;
        }
    }


const newPhone = new BuyPhoneForDad('XSMAx', 550, 128);
console.log('Class syntax example:- ' + newPhone.phoneModel, newPhone.price, newPhone.gb);

For the record, being a more experienced person does not give you any right to making a false assumption. The reason I even checked the solution was to know the insight I had is right or not. If I were you I would probably ask if you did not understand the question. I use more than one editor so I might have a little bit of idea of what is happening with the code.It’s not like I am writing this question without covering all the aspects. Please do reply with a optimistic and helpful answer.

Just some friendly advice for the future. If you post a link to a specific challenge, make the post title the URL of the same challenge, and ask about a solution to a challenge, those of us on the forum will probably assume that you want help with the specific challenge instead of general help on the topic of the challenge.

1 Like

Post Title :- Reference Error thrown for the solution provided in the challenge.

For this I provided two code snippets, one:- relevant to the challenge, two:- An example

function makeClass() {
class Vegetable {
    constructor(name){
        this.name = name;
    }
}
return Vegetable;
}
const veggies = new makeClass();
const veggiesConstructor = new Vegetable('carrot');
console.log(veggiesConstructor.name);

The above code snippet which is similar to the solution provided in challenge. When you run this code it gives Reference Error

Let me know if this makes things clear. I deeply apologize if I was harsh.
Merry Christmas!!

Vegetable is the class identifier, i.e. the class name. That is not what is returned by the makeClass function. It returns the class definition (i.e. all the class code inside the makeClass function). You need to use the correct identifier as the constructor. In the OP code that would be temp (new temp('brocolli')), in your latest post it would be veggies.

// OP code
// function and class remove for brevity

const temp = makeClass();
console.log(temp)

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

const veggies = new temp('brocolli');
console.log(veggies.name);
// brocolli
2 Likes