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
greg-tumolo
6961 gold badge7 silver badges30 bronze badges
5 Answers 5
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
answered Jul 27, 2022 at 14:58
Sign up to request clarification or add additional context in comments.
Comments
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
answered Jul 27, 2022 at 15:06
Comments
String.prototype.tittle = function(){
return (this[0].toUpperCase() + this.substring(1))
}
console.log('onimusha'.tittle()) // it returns Oonimusha, was expected Onimusha
answered Jul 27, 2022 at 16:00
Comments
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>
answered Jul 27, 2022 at 19:09
Comments
String.prototype.tittle = function() {
return this.charAt(0).toUpperCase() + this.substring(1)
}
console.log('onimusha'.tittle())
answered Jul 27, 2022 at 15:03
Comments
lang-js
Array('onimusha')->['onimusha']soArray('onimusha').shift()->'onimusha'