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

Instead of copying/pasting what you found somewhere, why not try to solve the challenge using the information and example in the challenge itself? We can help guide you to a solution if you first try.

The challenge instructions do not tell you to create a function named makeClass. Just do what the instructions ask. If you do not understand something in the instructions or the challenge description, feel free to reply back on this thread with your questions.

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

There is no way you just happen to the right your original solution (shown below):

Maybe you wrote the above or maybe you did not, but it is definitely something you could find in an older thread since that code used to be the solution a while back, before the challenge was changed. I have a lot of experience on the forum to know when someone copies/pastes something as a solution and when they write it own their own. I did not mean it as an insult, but it seems you have taken it that way.

As @JeremyLT stated above, you posted some code to a challenge and even referenced the challenge and you actually did not even ask a question. You just made the statement that you guessed something is wrong the the function that’s being called.

In your reply to my post, you did post the solution to the referenced challenge. Then you posted some more code but did not ask any questions about it, so it is difficult to say how we should respond or if we even need to respond to it.

In the future, please create topic titles which briefly summarize the help you need.

Thank you

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