Trying to replace random array characters within an array

This is my first ever question, please give me feedback if I suck at this. Thank you

I am trying to create a JavaScript terminal game from a paid course. The game is called ‘Find My Hat’.

One section of the problem requires me to create a static method called generateField(). This method generates a field, on the field there are 4 possible characters:

hat = '^';

hole = 'O';

fieldCharacter = '░';

pathCharacter = '*';

The fieldCharacter is the background for the game area. The game area is comprised of nested arrays. I haven’t had problems creating the field, but I also want to replace random fieldCharacters with holes. I tried this with Math.random and nested loops to iterate over the nested arrays. I have not been able to figure out how to get this to work.

Here is the code below, sorry if I missed any details, I will try respond to everyone I can.

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

    print() {
        this._field = field.join('n');
        console.log(this._field.replace(/,/g, ''))
    }

    static generateField(height, width) {
        let finalArray = []
        for (let i = 0; i < height; i++) {
            finalArray.push([fieldCharacter.repeat(width)]);
        }
        for (let i = 0; i < finalArray.length; i++) {
            let randomHole = () => {
                let result = Math.random() * width;
                return result;
                }
            for (let j = 0; j < finalArray[i].length; j++) {
                if (randomHole() > width - 2) {
                    finalArray[i][j] = hole;
                }
            }
        }
        return finalArray.join('n').replace(/,/g, '');
    }
}

console.log(Field.generateField(5, 10));

One Random Output:

O
O
░░░░░░░░░░
░░░░░░░░░░
O

Example for good output:

░░░░O░░░░░
░░░O░OO░░░
░░O░░░O░░░
░░░░O░░░░░
OO░░░░O░░O

Answer

I did some changes to your code and use the stackoverflow snippet to show you how it works

String.prototype.replaceAt = function(index, replacement) {
    return this.substr(0, index) + replacement + this.substr(index + replacement.length);
}

hat = '^';

hole = 'O';

fieldCharacter = '░';

pathCharacter = '*';

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

    print() {
        this._field = field.join('n');
        console.log(this._field.replace(/,/g, ''))
    }

    static generateField(height, width) {
        let finalArray = []
        for (let i = 0; i < height; i++) {
            finalArray.push(fieldCharacter.repeat(width));
        }
        //i dont know why you did this loop but i changed the inside for marking random holes
        for (let i = 0; i < finalArray.length; i++) {
            //pick random x and y
            let randomHoleX = Math.floor(Math.random() * width);
            let randomHoleY = Math.floor(Math.random() * height);
            //becouse you save array of strings you need to find the correct array and replace the char at the correct index.
            finalArray[randomHoleY] = finalArray[randomHoleY].replaceAt(randomHoleX, hole);
        }
        return finalArray.join('n').replace(/,/g, '');
    }
}

console.log(Field.generateField(5, 10));