Confusion with a 'Minesweeper' Style Matrix

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Good Day;

I am very confused by an issue which arises in the bellow code which is intended to randomly seed a board for minesweeper with bombs. Please focus in particular on the lines in between the multi-line comments. While I want for the board to be filled with some bombs, the bellow code fills the entire board with bombs.
Please note that were is written ‘\x’, treat it as ‘x’.

const GenBoard=(size=8)=>{
//Checks if input is a valid integer
if(!CheckIsInt(size)){size=8;}
else if(size>20||size<8){size=8;}
//Generates Board
let board=;
let yarr=new Array(size).fill(" “);
for(let i=0;i<size;i++){
board.push(yarr);
}
console.log(board);
/ Randomly Places A Number of bombs equal to the
value of ‘size’ around the board
/
for(let i=0;i<size;i++){
let found=false;
while(!found){
let x=Math.floor(Math.random()*board.size);
let y=Math.floor(Math.random()*board.size);
if(board[\x][y]==” "){
board[\x][y]=“B”;
found=true;
}
}
}
/ Prints out a 2x2 Array where each Element of Each
array is ‘B’ as opposed to only some of them
being ‘B’ and the remaining being ’ ’
/

console.log(board);

}

I am especially confused given how the bellow code works as I would intend:

let arr=;
for(i=0;i<10;i++){
arr.push(new Array(10).fill(" “));
}
console.log(arr);
for(let i=0;i<arr.length;i++){
let found=false;
while(!found){
let x=Math.floor(Math.random()*arr.length);
let y=Math.floor(Math.random()*arr.length);
if(arr[\x][y]==” "){
arr[\x][y]=‘B’;
found=true;
}
}
}
console.log(arr);

I would be very grateful for any advice, thank you in advance.