Lodash pad() trouble (I need project help :{ ) (SOLVED)

when I run the node test/pad.js, I pass all test except for (‘hi’, 6)
It keeps saying it’s returning ’ hi ’ (2 spaces) instead of ’ hi ’ (3 spaces) and I can’t figure out why.
I’m trying to write this without using the repeat() method as a personal challenge.

pad(str, len){

if (len < str.length){
  return str;
const half = Math.floor(len/2);
if (len%2 === 1){
  let newStr = '';
  for (let i = 0; i < half; i++){
    newStr = ' ' + str + ' ';
  return newStr + ' ';

if (len%2 === 0){
  let newStr = '';
  for (let i = 0; i < len/2; i++){
    newStr = ' ' + str + ' ';
  return newStr;


with each loop you’re overwriting ‘newStr’ with a single space concatenated with the string passed in and another single space. You’re not adding spaces. So the returned string will always be ‘space string space’.
If you replace the variable str by newStr and initialize newStr accordingly, that should be fixed.
Can you link the lessen? What do the instructions say?
Wasn’t ‘len’ supposed to be the length of the returned string including the passed string?


the instructions say -

Ideate: Again, as noted with previous game plans, there are many solutions to this problem. We chose this one for readability.

  • Add the .pad() method to the lodash object.
  • Check to make sure the target length is longer than the current string length. If not, return the unpadded version of the string.
    -Find the amount of padding to add to the start of the string by finding the difference between the target length and the string length, dividing by two, and rounding down the resulting number. We round down so that any uneven padding gets added to the end of the string, not the beginning, as specified in the instructions.

-Find the amount of padding to add to the end of the string by subtracting the string length and the starting padding length (calculated above) from the target length.

-Generate the padded string by adding the amount of starting padding and ending padding calculated above to each side of the current string.

  • Return the padded version of the string.

Once you have tried implementing this game plan in code, move on to the next step to see how we do it.

I somewhat see what you’re saying. So basically, I’m just creating a string with single spaces over and over in my loop instead of adding newStr into itself. Am i correct?

Yes, that’s right.

And also from the instruction my assumption that ‘len’ should be the length of the returned string is confirmed. That’s not what you’re attempting to do: Once you fixed the error mentioned above, you’ll return a string as long as len + str.length.

I love you so much right now.

So all fixed? You’re welcome…

yes,all fixed. Thanks again!

1 Like