 # Find Your Hat Challenge Project

I have a question about a couple of lines of code in the Codecademy solution, namely these two:

``````// Why do we declare percentage as 0.1 but then can also pass in an argument (0.2 in the example)?
static generateField(height, width, percentage = 0.1)  {
// Can someone please walk me through what the below line is doing? If I had to guess, I would say it declares a length for the new array (height parameter) and fills it starting from index 0, using map to perform a function on each element of the array to create new as may new arrays as the width parameter specifies
const field = new Array(height).fill(0).map(el => new Array(width));

// The arguments passed in to create the playing field are:
const myField = new Field(Field.generateField(10, 10, 0.2));
``````

Complete solution code:

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

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

class Field {
constructor(field = [[]]) {
this.field = field;
this.locationX = 0;
this.locationY = 0;
// Set the "home" position before the game starts
this.field = pathCharacter;
}

runGame() {
let playing = true;
while (playing) {
this.print();
if (!this.isInBounds()) {
console.log('Out of bounds instruction!');
playing = false;
break;
} else if (this.isHole()) {
console.log('Sorry, you fell down a hole!');
playing = false;
break;
} else if (this.isHat()) {
playing = false;
break;
}
// Update the current location on the map
this.field[this.locationY][this.locationX] = pathCharacter;
}
}

const answer = prompt('Which way? ').toUpperCase();
case 'U':
this.locationY -= 1;
break;
case 'D':
this.locationY += 1;
break;
case 'L':
this.locationX -= 1;
break;
case 'R':
this.locationX += 1;
break;
default:
console.log('Enter U, D, L or R.');
break;
}
}

isInBounds() {
return (
this.locationY >= 0 &&
this.locationX >= 0 &&
this.locationY < this.field.length &&
this.locationX < this.field.length
);
}

isHat() {
return this.field[this.locationY][this.locationX] === hat;
}

isHole() {
return this.field[this.locationY][this.locationX] === hole;
}

print() {
const displayString = this.field.map(row => {
return row.join('');
}).join('\n');
console.log(displayString);
}

static generateField(height, width, percentage = 0.1) {
const field = new Array(height).fill(0).map(el => new Array(width));
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const prob = Math.random();
field[y][x] = prob > percentage ? fieldCharacter : hole;
}
}
// Set the "hat" location
const hatLocation = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height)
};
// Make sure the "hat" is not at the starting point
while (hatLocation.x === 0 && hatLocation.y === 0) {
hatLocation.x = Math.floor(Math.random() * width);
hatLocation.y = Math.floor(Math.random() * height);
}
field[hatLocation.y][hatLocation.x] = hat;
return field;
}
}

const myfield = new Field(Field.generateField(10, 10, 0.2));
myfield.runGame();

``````