2

I created my own method which basically capitalizes the first alphabet of every word in a string.

However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where am I wrong?

String.prototype.toCapitalize = (str) => {
  let splits = str.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
h.toCapitalize();

3 Answers 3

3

How did you think that the first argument is the string? It is supposed to be this. Replacing str with this and works:

String.prototype.toCapitalize = function () {
  let splits = this.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
console.log(h.toCapitalize());

Sign up to request clarification or add additional context in comments.

Comments

2

A couple of issues. The function takes a parameter of str, but you're not calling it with any parameter. The conventional way to reference the instantiated object you're calling a method on is to use this, but you have an arrow function - better to use a standard function so you can use this:

String.prototype.toCapitalize = function() {
  let splits = this.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
console.log(h.toCapitalize());

But mutating the built-in objects is terrible practice - consider using a standalone function instead:

const toCapitalize = str => str
  .split(' ')
  .map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
  .join(' ');
let h = 'its a beautiful weather';
console.log(toCapitalize(h));

6 Comments

Thanks! And then I can't use arrow functions lol. Doesn't matter!
If you use a standalone function rather than mutating the built-in prototype, you can (and should) use an arrow function
Thanks for the alternative! Gotcha! Will be marking as answer in 5 minutes.
@Sanjay You could have asked me the same question, doh! :P
Don't mind but @CertainPerformace has less reputation than yours so I will be marking his as an answer. Justified :P
|
1

If you want to call toCapitalize as h.toCapitalize() then you need to use this.split(" "); as you are getting an error of str is not defined in console:

String.prototype.toCapitalize = function() {
  let splits = this.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let h = 'its a beautiful weather';
console.log(h.toCapitalize());

Else, if you want to use the parameter str in the toCapitalize function then you need to call it as st.toCapitalize(h) where st can be any string type value.

String.prototype.toCapitalize = function(str) {
  let splits = str.split(" ");
  let capitalize = '';
  splits.forEach((el) => {
    let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
    capitalize = capitalize + ' ' + result;
  });
  return capitalize;
}
let st ='';
let h = 'its a beautiful weather';
console.log(st.toCapitalize(h));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.