Lodash exercise pad(), passes test but throws error

Im working on the Lodash exercise pad() part https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/projects/lodash
Tasks 15-18
here is my code

  pad(string, length){
    if(string >= length){
      return string;
    const startPaddingLength = Math.floor((length - string.length) / 2);
    const endPaddingLength = length - string.length - startPaddingLength;
  const paddedStr =' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);
  return paddedStr;

It passes the test but still throws an error

 const paddedStr =' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);

RangeError: Invalid count value
    at String.repeat (native)
    at Object.pad (/home/ccuser/workspace/underscore-javascript-capstone/_.js:49:24)
    at Object.<anonymous> (/home/ccuser/workspace/underscore-javascript-capstone/test/pad.js:17:106)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)

I ended up copying the code from the video letter by letter and still get the same error and I really can’t figure it out. I’ve seen others in the forum that have gotten the same error on the same exercise but they had all made mistakes that kept them from passing the test. I pass the test but still get an error.

One particular invalid count value would be a negative number. Something to check for.

string and length are different types so this is going to give an incorrect result, most times.

string.length >= length

Thank you. That solved the problem. I still don’t understand the error though. It’s on a completely different line

1 Like

You’re welcome.

Is it possible that this is where the error is being thrown? Could there be a negative result?

length - string.length - startPaddingLength

Keep in mind that the previous validation wasn’t working to spec and permitted an illegal value to slip through.

1 Like