Invalid count value **Lodash Library**

Hello, here is my method I have created for the Lodash Library Project, I am getting an error of invalid count value. I have tried to use the value of the variables in place of the name of the variable and it passes the evenly spaced test but obviously will not pass the odd spaced padding test at the same time which is what I am looking to do but when using the variable name in place of variable value in the repeat method it returns an invalid count value error.

Thank you

 pad: function (string, length) {
    let startPad = Math.floor((length - string.length) / 2);   
  
  let endPad = length - string.length - startPad; 
   
 let space = `${' '.repeat(startPad)}${string}${' '.repeat(endPad)}`;
   
 if (string.length < length) {
     // return `${startPad}${string}${endPad}`;  
     return space;
    } else if (string.length > length) {
      return string;
    } else if (string.length === length) {
      return string;
    }
      //return `${startPad}${string}${endPad}`;
      return space;
  }

This version worked …

` pad: function (string, length) {
if (string.length >= length) {
return string;
}

let startPad = Math.floor((length - string.length) / 2);
let endPad = length - string.length - startPad;
let space = ’ '.repeat(startPad) + string + ’ '.repeat(endPad);
return space;
}`

I am confused as to why that one passes all tests with no error and the first one does not

Hello, @kbgratto.

Consider the order in which the code is executed in each case. In your first version what happens if we call your pad function with the following arguments?

_.pad('banana', 4);

Consider:

console.log('Hi'.repeat(4)); // prints HiHiHiHi
console.log('Hi'.repeat(0)); //prints a blank line
console.log('Hi'.repeat(-1))://prints RangeError: Invalid count value at String.repeat (<anonymous>)