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.

Discussion (0)