Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Last active August 7, 2018 16:49
Show Gist options
  • Save renatocassino/cce62e558148c211a1fcf36e44c080c9 to your computer and use it in GitHub Desktop.
Save renatocassino/cce62e558148c211a1fcf36e44c080c9 to your computer and use it in GitHub Desktop.
2048-addNumber.js
// @param 2 in format [column:int, line:int]
// @param number with default value 2
const addNumber = (board, [columnIndex, lineIndex], number=2) => {
const line = board[columnIndex]; // Get line to change
const newLine = [
...line.slice(0, lineIndex), // Getting all values before index to change
number, // Add number to array
...line.slice(lineIndex + 1, line.length), // Getting all values after index to change
];
return [ // Creating new board
...board.slice(0, columnIndex), // Lines before index
newLine, // New line
...board.slice(columnIndex + 1, board.length), // Lines after index
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment