Home > Blockchain >  I need to create a method that upper cases only the first string of an array (Javascript)
I need to create a method that upper cases only the first string of an array (Javascript)

Time:07-27

String.prototype.tittle = function(){
    return (this.split('')[0].toUpperCase())   ((Array(this)).shift())
}

console.log('onimusha'.tittle())   // it returns Oonimusha, was expected Onimusha

CodePudding user response:

Based on your expected output, you want this.

String.prototype.tittle = function(){
    return this[0].toUpperCase()   this.slice(1)
}

console.log('onimusha'.tittle())   // it returns Oonimusha, was expected Onimusha

CodePudding user response:

String.prototype.tittle = function() {
    return this[0].toUpperCase()   this.substring(1)
}

console.log('onimusha'.tittle())

CodePudding user response:

try:

String.prototype.tittle = function() {

  // break up string into individual characters
  let chars = this.split('');

  // uppercase the first character and save it back to the first element 
  chars[0] = chars[0].toUpperCase();

  // then piece back the string and return it
  return chars.join('');
}

console.log('hello'.tittle()); // Hello
  • Related