Title Case a Sentence

Heggy Castaneda
Jul 22, 2017 · 2 min read
# Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

Capitalize first letter in each word a string. Start with string and end with string.

I want to break down what FCC Basic Code Solution:

String.prototype.replaceAt = function(index, character) {
return this.substr(0, index)
+ character
+ this.substr(index + character.length);
};
function titleCase(str) { var newTitle = str.split(' ');
var updatedTitle = [];
for (var st in newTitle) { updatedTitle[st] =
newTitle[st]
.toLowerCase()
.replaceAt(0, newTitle[st].charAt(0).toUpperCase());
}
return updatedTitle.join(' ');
}

First, let’s brush upon for..in loop.

var fccCamperObj = {
nameKey: "heggy",
hobbyKey: "drinking boba tea",
catLoverKey: true
};
// iterate through obj, return key => value
// ex: name => heggy, hobby => drinking boba tea, catLover => true
// generic format of for..in loop: for (var key in object)
for (var key in fccCamperObj) {
// key below is same variable key inside of for loop parenthesis console.log(key + ' => ' + fccCamperObj[key]);
}

Note 1: key is a variable therefore we use bracket notation (not dot notation) to get the value. fccCamperObj[key]

Note 2: key is a variable that gets assigned to each property key inside of object fccCamperObj.

output:

nameKey => heggy
hobbyKey => drinking boba tea
catLoverKey => true


function titleCase(str) {  var newTitle = str.split(' ');  var updatedTitle = [];  // for...in obj iteration for (var key in obj)  for (var position in newTitle) {  // newTitle[position]  // .replaceAt custom-made method called with index = 0 and first char becoming upperCase ('T')  // newTitle[position].charAt(0) => extract single charAt position 0 (output: t from tea)  // updateTitle[position] each position get new word inside arrayupdatedTitle[position] = newTitle[position].toLowerCase().replaceAt(0, newTitle[position].charAt(0).toUpperCase());}// .join change updateTitle back into string from array of str// .join(' ') joins with spacing in betwnreturn updatedTitle.join(' ');}// when updatedTitle[position] is called this custom-made replaceAt method is called.
// Let's say we are on 'tea' on our array.
// newTitle[position].toLowerCase().replaceAt(0, 'T')
// newTitle[3] = tea, .charAt(0) grab first element of
// tea => 't', then make it toUpperCase letter 'T'
// note: newTitle[position].charAt(0).toUpperCase() => 'T'
String.prototype.replaceAt = function(index, character) {// 'this' stands for newTitle[position] element of object in our array
// since it is method chained in a following manner
// newTitle[position].toLowerCase().replaceAt(0, 'T')
// newTitle[position] => newTitle[3] = tea in our current example.
// this.substr (0, index = 0)
// substr (0, number of char to extract = 0) => substr(0,0) means start at 0 but extract none(0)
// character = 'T' from newTitle[position].charAt(0).toUpperCase() means grab first character and make it upperCase
// this.substr(0 + 1 = 1) => extract string start at index 1, (no second argument defaults grab the rest)
// output: Teareturn this.substr(0, index) + character + this.substr(index + character.length);};// uncomment debugger below to run through it in console
// debugger;
titleCase("I'm a little tea pot");

Heggy Castaneda

Written by

like to build things

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade