Lodash challenge pad()

Hello all, I am having trouble seeing why my solution to the lodash pad() challenge fails to pass.

I get this error:

1 - _.pad() is defined - Passed!
2 - Returns evenly-padded strings - Passed!
3 - Returns oddly-padded strings with extra padding on right side - Failed: _.pad("hi", 5) returned   hi   instead of  hi  .
4 - Returns strings longer than provided length un-padded - Passed!

It’s telling me i have three space characters either side of the word ‘Hi’ instead of two.

However when I run the function on its own, I get the two spaces. Here is my test function (using asterisks instead of spaces for ease of counting!).

const pad = (string, length) => {
  if (string.length >= length) {
    return string;
  };
  while (string.length < length) {
    string = '*' + string + '*';
  };
  return string;
}
 
for (let i = 0; i < 10; i++) {
  console.log('string length is ' + (i+1) + ' ' + pad('Hi', i+1));
}

and here is the code derived from it which does not pass in the exercise:

pad(string, length) {
    if (string.length >= length) {
      return string;
    }
    while (string.length < length) {
      string = " " + string + " ";
    }
    return string;
  },

Any ideas why the two examples give different results?

(PS - just noticed string is showing here in colour - i’m guessing it’s a keyword - maybe should have used another variable name).

Please post a link to the lesson so we can see the instructions.
Your function will always return the same amount of spaces on either side.
What do the instructions say is supposed to happen for arguments like this?:

pad('hi', 5)

The returned string would have a length of 6 rather than 5. Is that expected?

Hello, and thanks for replying. Here is a link to the exercise:

https://www.codecademy.com/projects/practice/lodash

The specification says " * .pad() adds spaces evenly to both sides of the string to make it reach the desired length.

  • Extra padding is added to the end of the string if an odd amount of padding is required to reach the specified length."

In my test function, ‘Hi’ is padded with 2 asterisks which seems to fulfill the spec. When I add it to the exercise, it fails to pass. :frowning:

The instructions you quoted instruct you to add extra padding to the right side if the length argument is odd while the input string’s length is even. For the example (with asterisks rather than spaces) that would mean that the returned string looks like this:

pad('hi', 5) // *hi**

The string’s length must be 5. One space on the left, two on the right side.

1 Like

Thank you.

In the light of your comment, I have re-written my code, which now passes :slight_smile: :

 pad(string, length) {
    if (string.length >= length) {
    return string;
  };
  do {
    string = ' ' + string + ' ';
    if (string.length === length-1) {
      string = string + ' ';
    };
  } while (string.length < length);
  return string;
  }

For what it’s worth, I think the specification is poorly worded:

" * .pad() adds spaces evenly to both sides of the string to make it reach the desired length."

and,

" * Extra padding is added to the end of the string if an odd amount of padding is required to reach the specified length."

The second case seems to contradict the first. In the second case, you are not adding spaces evenly to make it reach the desired length. You are adding them unevenly.