Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Created July 16, 2018 07:38
Show Gist options
  • Save dschinkel/828cf5100237d30cc62c7921850b318f to your computer and use it in GitHub Desktop.
Save dschinkel/828cf5100237d30cc62c7921850b318f to your computer and use it in GitHub Desktop.
Medium Blog Post - Data Hiding, Instances, and Better Testing with JS Factories and Closures
let board;
resetToEmptyBoard();
function getBoard() {
return board;
}
function cellIsEmpty(position){
const empty = board[position] === ' ';
return empty;
}
function getEmptyCells(){
const emptyCells = board.filter(cell => {
return cell === ' '
});
return emptyCells;
}
function resetToEmptyBoard(){
board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '];
}
function setBoard(newBoard){
board = newBoard;
}
function isFull(board){
const emptyCells = board.filter(cell => {
return cell === ' '
});
return emptyCells.length === 0;
}
const Board = {
isFull,
getBoard,
getEmptyCells,
cellIsEmpty,
resetToEmptyBoard,
setBoard
};
export default Board;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment