[N/A] Codecademy environment shows me: TypeError

Hi.

I’m practicing on Lodash Project, and when I’m trying with _pad(), codecademy environment shows me: TypeError: string.padStart is not a function.

However, in my computer it perfectly works, and node.js shows me this:
1 - _.pad() is defined - Passed! 2 - Returns strings longer than provided length un-padded - Passed! 3 - Returns evenly-padded strings - Passed! 4 - Returns oddly-padded strings with extra padding on right side - Passed!

I just copied the method I wrote:

pad(string, number){
        const differene=number-(string.length);
        const paddNumber=differene/2;
        if(differene%2===0){
          const first=string.padStart((string.length)+paddNumber);
          const second=first.padEnd((first.length)+paddNumber);
          return second
        } else{
          const start=Math.floor(paddNumber);
          const end=differene-start;
          const first=string.padStart((string.length)+start);
          const second=first.padEnd((first.length)+end);
          return second
        }
      }

I ran this slightly revised code and got the same result…

  pad (string, number) {
    const difference = number - string.length;
    const padNumber = difference / 2;
    if (difference % 2 === 0) {
      const first = string.padStart(string.length + padNumber);
      const second = first.padEnd(first.length + padNumber);
      return second
   } else {
      const start = Math.floor(padNumber);
      const end = difference - start;
      const first = string.padStart(string.length + start);
      const second = first.padEnd(first.length + end);
      return second
    }
  },

Either this module has a very old version of Node.js behind it, or the function has been disabled. It has been supported since Node.js 8.0.

If you want to pass this step, then try another approach that does not use the .pad___() methods.

You can check the version of Node in use from the terminal:

image

As you can see, it’s v7.10.1

2 Likes

Looks like the LE is due for an upgrade. The current version is 13, released in October, 2019.

1 Like

Well, guess I’m gonna keep going XD

Hope they update it.

Did you try another approach? Don’t give up.

1 Like

Sure I did. :ok_hand:t4:

1 Like

Anything like this?

  pad (str, len) {
    const n = str.length;
    if (len <= n) return str;
    const m = Math.floor((len - n) / 2);
    str = `${' '.repeat(m)}${str}`;
    return `${str}${' '.repeat(len - str.length)}`
  }, 
1 Like

whaaaaat. That’s way better than mine.

pad(string, number){
        const x=' ';
        const differene=number-(string.length);
        const paddNumber=differene/2;
        if(differene%2===0){
          const word=string.split('');
          for(let i=0; i < paddNumber; i++){
            word.unshift(x);
            word.push(x);
          };
          const wordString=word.join('');
          return wordString
        } else{
          const word=string.split('');
          const start=Math.floor(paddNumber);
          const end=differene-start;
          for(let i=0; i < start; i++){
            word.unshift(x);
          };
          for(let i=0; i < end; i++){
            word.push(x);
          };
          const wordString=word.join('');
          return wordString
        }
      },
1 Like

If it works, then it can be refactored, I always say, write code that works, then look for simpler ways to make it do the same thing. We don’t get there using other people’s code. Keep working and enjoying the fruits of your own labors.

1 Like

What you said about using other people’s code it’s totally true. I’ll keep working!

Thank you!

1 Like