Find Your Hat Project - Help!

Dear folk, I do need your help achieving success in this project!

Did not yet start the playable part, cause I’m stuck on generating the field. Actually I thought it was ok but JS is throwing me back this error (code is starting right below it):
+++++++++++++++++++++++++++++++++++++++++
/home/ccuser/workspace/find-your-hat/main.js:37
const gameField = Field.generateField(5, 5);
^

TypeError: Field.generateField is not a function
at Object. (/home/ccuser/workspace/find-your-hat/main.js:37:25)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
+++++++++++++++++++++++++++++++++++++++++

const prompt = require('prompt-sync')({ sigint: true });

const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';

class Field {
  constructor(field) {
    this._field = field;
  }

  print() {
    for (const element of this._field) {
      console.log(element.join(' '));
    }
  }

  generateField(width, height) {
    const characters = ["O", "░", "░" , "░", "░"];
    let field = [];
    for (let i = 0; i < height; i++) {
      let row = [];
      for (let j = 0; j < width; j++) {
        let addCharacter = characters[Math.floor(Math.random() * characters.length)];
        row.push(addCharacter);
      }
      field.push(row);
    }
    let randomRow = Math.floor(Math.random() * height);
    let randomChar = Math.floor(Math.random() * width);
    field[randomRow][randomChar] = "^";
    return field;
  }

}

const gameField = Field.generateField(5, 5);

gameField.print();

The class Field does not have a static method .generateField so Field.generateField(5, 5) won’t work.
And .generateField(5, 5) does not create a Field object, it returns a 2-dimensional array.
And only Field objects have a .print method ( … arrays do not).

You need to make a Field object (an object of the class Field) for this to work:

const gameField = new Field();
gamefield._field = gameField.generateField(5, 5);
gameField.print();

Or, if you make the generateField a static method, you could do:

const fieldArray = Field.generateField(5, 5);
const gameField = new Field(fieldArray);
gameField.print();
1 Like

Went for the second option, making generateField a static method. You nailed it!
Do thank you so much buddy :smile: :pray: