Lodash Project - pad

Hi guys, well I’m struggling with the fourth excercise of the Lodash Project. I watched the Help Video several times but I can’t find my mistake:

const _ = {
   pad (string, length){
    if (length <= string.length){
      return string;
    };
    const addToB = Math.floor((length - string.lenght) / 2);
    const addToE = length - string.length - addToB;
    const paddedString = ' '.repeat(addToB) + string + ' '.repeat(addToE);
    
    return paddedString;
  },
  
};

Should be string.length instead of string.lenght.

2 Likes

Thank you so much!
Dumb mistake haha I wasn’t able to see that after some time of coding…

You’re welcome :slight_smile:

Hi there!

I made the solution, which works properly in other enviromnet, but it didn’t pass the test in codecademy enviroment. Could you help me please to figure out the reason.

pad(string, length) {
    if (string.length >= length) {
      return string;
    }
    
    if (string.length < length) {
      let paddingToStart = Math.floor((length - string.length)/2);
      let paddingToEnd = Math.ceil((length - string.length)/2);
      let stringWithAddStart = string.padStart(paddingToStart + string.length);
      return stringWithAddStart.padEnd(length);
    }
  }

Did you read the error message? It isn’t complaining about pad
The error says you’re calling something that isn’t a function.
So, was there supposed to be a function there? If so maybe you overwrote it. Or, maybe that function is somewhere else. Either way, you mixed something up, and the error message is pointing out where things don’t match.

Yes, I read the error message. But I still have no idea of the reason. I didn’t use such name of functions or variables anywhere. padStart and padEnd - are the methods, which I’ve found in the documentation by myself (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd). They seem to be suitable for this task, but may be there is something wrong with my using these methods?

…you do, you say so yourself don’t you >_<
so from there you can reason about why it would or wouldn’t be there. Is it something your code did or is it really a difference in the environment
you can rule out that it’s a problem with your code by doing nothing other than ''.padStart to see if that’s a function (and compare with your local environment)
so, then, is it something codecademy removed, or is it just not there in the first place? mdn lists versions where things start being supported in various environments, for string.padStart in node that’s 8.0.0, which version does codecademy use?

If I am not mistaken the version, which codecademy uses is 7.10.1. I suppose this is the answer!%D0%92%D0%B5%D1%80%D1%81%D0%B8%D1%8F

Yeah.
I mean, if you really wanted access to some library that did all the work then… const _ = require('lodash')
Or even not all the work, but maybe there’s something you’d want to use when expressing your code. But the point is kinda to build up to it yourself from nothing-too-fancy.

1 Like

it’s because you are trying to use lodash methods which do not exist in the codecademy library. we are recreating them in this project so you are calling a function which does not exist in your system.