Bruno
Bruno

Reputation: 77

I need to create a method that upper cases only the first character of a string (Javascript)

My incorrect method:

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

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

Upvotes: 1

Views: 84

Answers (5)

greg-tumolo
greg-tumolo

Reputation: 696

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

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

Upvotes: 1

zer00ne
zer00ne

Reputation: 44088

A way to sentence case a string:

Example A

String.prototype.s = function() {
  return this.charAt(0).toUpperCase() + this.slice(1);
}

console.log(`this is a sentence.`.s());

Here's a class that formats strings in sentence, title, and yelling case.

Example B

Details are commented in example

/**
 * A class that formats string casing 
 * @class
 */
class Case {
  /**
   * Assign a given string as an object.
   * @constructor
   * @param {string} string - A string
   * @default {string} - An empty string
   */
  constructor(string = '') {
    this.string = string.replace(/[\s]+/g, ' ').trim();
  }

  /**
   * Format a given string as a sentence.
   * Criteria: text begins with an uppercase letter.
   * @param {string} text - A string
   */
  s(text) {
    if (text) this.string = text;
    return this.string.charAt(0).toUpperCase() + this.string.slice(1);
  }

  /**
   * Format a given string as a title.
   * Criterea: each word's first letter is uppercase, exceptions are
   * any word that's not the first or last and is less than 4 
   * characters. Loosely based on MLA.
   * @param {string} text - A string
   */
  t(text) {
    if (text) this.string = text;
    let array = this.string.split(' ').map((str, idx) => {
      if (idx != 0 && idx != this.string.length - 1 && str.length < 4) {
        return str;
      }
      return this.s(str);
    });
    return array.join(' ').replace(/\./g, '');
  }

  /**
   * Format a given string as a shout.
   * Criteria: all characters are uppercase.
   * @param {string} text - A string
   */
  y(text) {
    if (text) this.string = text;
    return this.string.toUpperCase();
  }
}

// Text from DOM
const strings = [...document.querySelectorAll('p')]
  .map(p => new Case(p.textContent));
console.log(strings[0].s());
console.log(strings[1].t());
console.log(strings[2].y());

// Text passed as a parameter
const c = new Case();
console.log(c.s('this is a sentence.'));
console.log(c.t('this is a title.'));
console.log(c.y('this is yelling!'));
<p>this is a sentence.</p>
<p>this is a title.</p>
<p>this is yelling!</p>

Upvotes: 1

Deepesh kumar Gupta
Deepesh kumar Gupta

Reputation: 896

String.prototype.tittle = function(){

    return (this[0].toUpperCase() + this.substring(1))
}

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

Upvotes: 1

martin66
martin66

Reputation: 387

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

Upvotes: 1

Samathingamajig
Samathingamajig

Reputation: 13283

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

Upvotes: 1

Related Questions