DEV Community

Cover image for Challenge: Create a `pad` function without using loops!
Gio
Gio

Posted on

Challenge: Create a `pad` function without using loops!

Photo Credit: Kylie Fitts / www.kyliefitts.com & https://unsplash.com

In any language, implement a function pad that takes a value and conditionally pads it with n number of padding:

const padded = pad({ value: 'πŸ‘‹', padding: '*', requiredLength: 4, }) console.log(padded) // --> ***πŸ‘‹ ////////// // Case 2: do not pad a value whose length is equal to `requiredLength` // const padded = pad({ value: 'πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹', padding: '*', requiredLength: 4, }) console.log(padded) // --> πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹ ////////// // Case 3: do not overwrite a value that is longer than `requiredLength` // const padded = pad({ value: 'πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹', padding: '*', requiredLength: 4, }) console.log(padded) // --> πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹πŸ‘‹ 
Enter fullscreen mode Exit fullscreen mode

Submit your solutions down below! πŸ‘‡πŸ‘‡πŸ‘‡

Remember, your solution cannot use any sort of loop construct such as while, do, or for!

WARNING: Here is my solution in typescript.

Top comments (0)