Find yr hat function vs staic method issues

I can see there’s a lot of find your hat threads, bt AFAICT nobody is asking what I am asking, so…

I need some help. I can manage to get the generateField to work as a function outside the class, but when I move it inside the class I get all kinds of type errors. If I try to put just the first part of it (creating a field of just ‘░’ then I get undefined. My problem is I don’t know if my code is wrong or if I am just making a mistake in how I try to test my part finished class. I don’t want to just continue and hope that it will all work in the end, that seems doomed to fail.

Here’s a copden of the function on it’s own and working

Here’s a codepen of it in the class and not working (or me not knowing how to test it properly)

Note that in first codepen I had to use field.length and field[0].length in place of rows and cols as they’re arguments in the static function, perhaps this is the problem, IDK. I would really appreciate some help as I have been at this for a few days on and off and I’m making slow progress. :grimacing:


So here I am again :wink:
This is not yet the solution, I guess. Let’s start with the first issue I noticed…
I have the impression that you’re mixing up the principle of classes and objects. You’re using class here and you should create an instance first:

const field = new Field(5, 5, 10);

Your constructor takes one argument: field. But you’re creating the field with a method of that class, so you cannot pass the field as an argument for the creation of the instance already.
If you pass three arguments for the instance creation, your constructor should have three parameters:

constructor(rows, cols, percent) {
    this.rows = rows;
    this.cols = cols;
    this.percent = percent;

You could also create the instance without arguments and create the field inside the constructor by calling the method, but I think you want to be flexible with the size of the field, right?

1 Like

I do want to be able to change the field size, at least for now. I see what you’re saying about the arguments. I’ll go away and see how far I get if I add the extra args. to the constructor. Thanks again for taking a look at this. I’ll post any progress I make.

1 Like

I wouldn’t expect anything soon, I think I have confused myself completely. I’m actually considering shelving this and just going back to the begining of classes and doing it all over again, at this point I don’t think i understand anything. Thanks for helping, I think I need to untangle the mess I have got myelf into.

1 Like

Find your hat is definitively a challenging project. So going back and coming back at this later with a fresh mind is probably a good idea. Good luck!

1 Like

Thanks for your time

1 Like

A quick aside, not wanting to drag this up again before I feel able to get a lot further on my own, but I took what you said about initializing the array properly and I used array.from to make an empty array from the rows, cols arguments and it all works now, even the holes and hat placement… weird how the other one worked outside the class but not in it.

Anyway, I’m still going to redo classes or I’ll just get stuck on the next part of the FYH project. Just wanted to let you know that you’d helped me crack that part and I feel a lot better about it now.

No worries. Glad I could help.

Edit: Deleted the part about assigning array items in your function. I eventually got a valid output with your version as well. Sorry for the confusion.

1 Like