Creating a game board - minesweeper


#1

Hi!
My code for creating a game board is printing the game board with the line ‘undefined’ at the bottom:

CeciliaertysMBP:src ceciliadougherty$ node minesweeper.js
Current board:
| |
| |
| |
undefined

I’m writing the code based on lesson. Below is the code I wrote in minesweeper.js. Why is this printing ‘undefined’ in the console?

const printBoard = board => {
console.log('Current board: ‘);
console.log(board[0].join(’ | ‘));
console.log(board[1].join(’ | ‘));
console.log(board[2].join(’ | ‘));
}
const board = [
[’ ', ’ ', ’ ‘],
[’ ', ’ ', ’ ‘],
[’ ', ’ ', ’ ']
];
console.log(printBoard(board));

THANKS!


#2

undefined is the default returned value of a function, by calling console.log on your printBoard function call you log the returned result (which is undefined) of printBoard function


#3

I wish I understood what you are talking about, but I honestly can’t say
that I do understand.
The default value of a function is undefined. Even after you give it a
value? I thought ‘const board = board => …’ gave it a value.
Or is it because a function is not a variable.
Ok, a little more confused now.


#4

this is different:

from:

returned is key here, function always have to return a value. So undefined is the default returned value

to log the returned result you need to call console.log() on printBoard function call, which you do here:


console.log(printBoard(board));

but do you want to log the returned result?


#5

Ok. thank you. It’s becoming a little clearer now. I appreciate your
response!
cd


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.