_.pad help

here is my code, which I have checked dozens of times and it matches exactly what I have seen, but it still doesn’t work. Please help me see what I’m missing.

ad(string, length){
if (length <= string){
return string;
var startPaddingLength = Math.floor((length - string.length) / 2);
var endPaddingLength = length - string - startPaddingLength;
var paddedString = ’ '.repeat(startPaddingLength) + string + ’ '.repeat(endPaddingLength);
return paddedString;

Here is the error code:

var paddedString = ’ '.repeat(startPaddingLength) + string + ’ '.repeat(endPaddingLength);

RangeError: Invalid count value
at String.repeat (native)


I think you’re supposed to press space between curly brackets and parentheses

Since the error is saying that it’s an invalid count value and it’s pointing at your call to the .repeat() method, I suggest you use console.log() to print out the variables you’re calculating for the lengths before you pass them to .repeat(). This could help in your debugging



Is the number what you’re expecting? If it isn’t, you can look back at how you assign that value and try to figure out what’s going on.

For example, if the number is negative, how could that happen? The condition at the beginning should have already prevented the rest of the code from running if the length is less than the length of the string passed in. If it isn’t working as expected, then it needs to be examined closer.

Another hint

You’re currently comparing the number length to string, which could be a string of anything.

So right now, if pad was called with 'green' and 2, your condition looks like:

if (2 <= 'green') {

What can you use to get the length of string rather than just trying to compare the actual string to a number?

After you fix the first issue, you’ll most likely get another error that looks similar. It isn’t the same. Look very closely at it and consider how you fixed the first one.