From fba7363fa40583f20fb2bacbc0ea42cbb82b4f72 Mon Sep 17 00:00:00 2001 From: marleyrae Date: Tue, 27 Jun 2023 18:34:53 -0700 Subject: [PATCH] Write .pad() --- lodash/_.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lodash/_.js b/lodash/_.js index 9c9880a..810a28d 100644 --- a/lodash/_.js +++ b/lodash/_.js @@ -1,4 +1,4 @@ -let _ = {} +const _ = {} /* --------------------------------------------------------------- clamp ---- */ @@ -13,7 +13,7 @@ _.inRange = function (num, start, end) { } if (start > end) { - let s = start + const s = start start = end end = s } @@ -25,5 +25,18 @@ _.inRange = function (num, start, end) { _.words = (string) => string.split(' ') +/* ----------------------------------------------------------------- pad ---- */ + +_.pad = function (string, length) { + if (string.length >= length) { + return string + } + + const addToStart = Math.floor((length - string.length) / 2) + const addToEnd = length - string.length - addToStart + + return ' '.repeat(addToStart) + string + ' '.repeat(addToEnd) +} + // Do not write or modify code below this line. module.exports = _