Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Last active August 11, 2018 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatocassino/930c93dcfced22662e2ec3851cca29d9 to your computer and use it in GitHub Desktop.
Save renatocassino/930c93dcfced22662e2ec3851cca29d9 to your computer and use it in GitHub Desktop.
2048-generateArrayPad.js
// Generate array with size
const generateArraySize = (size, defaultValue = 0) => Array.from({ length: size }, () => defaultValue);
// Function receive an array and the size
const arrayPad = (arr=[], size=4) => {
return [...arr, ...generateArraySize(size - arr.length)];
};
console.log(arrayPad([2,4,8], 4));
console.log(arrayPad([2], 4));
console.log(arrayPad([2,4,8,16], 4));
// Response
//[ 2, 4, 8, 0 ]
//[ 2, 0, 0, 0 ]
//[ 2, 4, 8, 16 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment