Last active
August 11, 2018 23:14
-
-
Save renatocassino/930c93dcfced22662e2ec3851cca29d9 to your computer and use it in GitHub Desktop.
2048-generateArrayPad.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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