# Confusion Regarding Randomly Filling A Matrix

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.

I’m mystified by the `\` in the index…

``````if(board[\x][y]==” "){
board[\x][y]=“B”;
found=true;
}
``````

Can you fill us in as the role of that token?

As mentioned at the top of the post, please treat all instances of ‘\x’ as simply x.

Not putting in the backslash caused it to become a checkbox: .

That’s what I get for running headlong into the code. When posting code samples in the forums use the formatting tool, `</>` in the editor toolbar.

The new user section contains instructions on how to format when posting code.

Arrow functions cannot use default parameters.

I’ve changed it, but it still refuses to work, I’m very confused and have even copied and pasted the code which did work, but changed the variables.

I apologise for my further confusion.

Try posting your code again in a reply. Be sure to paste it between triple back ticks,

``````
code goes here
``````

‘’’
function GenBoard(size=8){
if(!CheIsInt(size)){size=8;}
else if(size>20||size<8){size=8;}
let board=;
let yarr=new Array(size).fill(" ");
for(let i=0;i<size;i++){
board.push(yarr);
}
console.log(board)

``````for(let i=0;i<size;i++){
let found=false;
while(!found){
let x=Math.floor(Math.random()*size);
let y=Math.floor(Math.random()*size);
if(board[x][y]==" "){
board[x][y]='B';
found=true;
}
}
}
console.log(board);
``````

}
‘’’

The back tick is usually found on the same key as the tilde.

``````function GenBoard(size=8){
if(!CheIsInt(size)){size=8;}
else if(size>20||size<8){size=8;}
let board=[];
let yarr=new Array(size).fill(" ");
for(let i=0;i<size;i++){
board.push(yarr);
}
console.log(board)

for(let i=0;i<size;i++){
let found=false;
while(!found){
let x=Math.floor(Math.random()*size);
let y=Math.floor(Math.random()*size);
if(board[x][y]==" "){
board[x][y]='B';
found=true;
}
}
}
console.log(board);
}
``````

Is `CheckIsInt()` a function of your creating?

Yes, because Javascript has no ability to discern between different types of numbers, I made this funciton:

``````function CheIsInt(n){
let IsInt=true;
let test=n.toString();
for(let i=0;i<test.length;i++){
if((test.charCodeAt(i)<48)||(test.charCodeAt(i)>57)){
IsInt=false;
return IsInt;
}
}
return IsInt;
}
``````

Please however, focus on the code that I am having issue with, as I am very confused.

``````board[4][5] = "X"
"X"
board
``````

The rows are not independent. Might be you’ll have to find some other way to construct the rows so they are independent of each other.

``````    for(let i=0;i<size;i++){
board.push(new Array(size).fill(" "));
}
``````

Thank very much- just to make sure I never make the same mistake again, what is the difference between creating the rows as a variable as I did, and pushing an unnamed row as you did?

1 Like

Using a variable means every row is the same object.

Aside

Let’s create an integer checker that doesn’t rely on character recognition, but rather uses maths.

``````is_int = x => ! (x % 1)
``````

That’s the core recognition schema.

What if we want to return `false` (for `NaN`, et al)? We turn the return into a logical expression…

``````is_int = x => ! (x % 1) && parseInt(x, 10) || false
``````
`````` > is_int('a')
<- false
> is_int(42)
<- 42
> is_int(3.42)
<- false
``````

I wasn’t aware of the parseInt function- your solution is far more elegant and I think I’ll use that instead. Thank you very much for your help and input, I’ve benefited greatly.

1 Like

OH YES! Javascript is weird; I’m not new to programming having done stuff in C/C++ and Python, so it was truly confusing- it’s deceptive that many modern programming languages are so similar in style and function, but learning the idiosyncrasies of each separates learners such as myself from the professionals I hope to one day superseded. Thank you for this reminder.

1 Like

There is one minor flaw… The code looks like but is not truly a predicate function since it returns the value when true, not `true` as a predicate would.

`````` size = is_int(size);
``````

The function does return `0` as a value, as well as any negative number. Usage cases would need to further examine these.

Look up both `parseInt` and `parseFloat` on MDN to see their full documentation. Using these functions we can convert numbers from any base to a base 10 number.

``parseInt('FF', 16)  =>  255``