Fix typos and add ASCII link

This commit is contained in:
Charlie Moore
2021-10-04 09:53:19 -04:00
parent 4ea13dee13
commit 4ab047ddc5

View File

@ -3,17 +3,17 @@
*/ */
/** /**
* The TitleCaseConversion converts a string into a title case string. * The titleCaseConversion function converts a string into a title case string.
* @param {String} inputString input string * @param {string} inputString The input string which can have any types of letter casing.
* @returns {String} * @returns {string} A string that is in title case.
*/ */
const titleCaseConversion = (inputString) => { const titleCaseConversion = (inputString) => {
// Extact all space seprated string. // Extact all space seprated string.
const stringCollections = inputString.split(' ').map(word => { const stringCollections = inputString.split(' ').map(word => {
let firstChar = '' let firstChar = ''
// Get a character code by the use charCodeAt method. // Get the [ASCII](https://en.wikipedia.org/wiki/ASCII) character code by the use charCodeAt method.
const firstCharCode = word[0].charCodeAt() const firstCharCode = word[0].charCodeAt()
// If the character code lies between 97 to 122 it means they are in the lower case so convert it. // If the ASCII character code lies between 97 to 122 it means they are in the lowercase so convert it.
if (firstCharCode >= 97 && firstCharCode <= 122) { if (firstCharCode >= 97 && firstCharCode <= 122) {
// Convert the case by use of the above explanation. // Convert the case by use of the above explanation.
firstChar += String.fromCharCode(firstCharCode - 32) firstChar += String.fromCharCode(firstCharCode - 32)
@ -22,9 +22,9 @@ const titleCaseConversion = (inputString) => {
firstChar += word[0] firstChar += word[0]
} }
const newWordChar = word.slice(1).split('').map(char => { const newWordChar = word.slice(1).split('').map(char => {
// Get a character code by the use charCodeAt method. // Get the ASCII character code by the use charCodeAt method.
const presentCharCode = char.charCodeAt() const presentCharCode = char.charCodeAt()
// If the character code lies between 65 to 90 it means they are in the upper case so convert it. // If the ASCII character code lies between 65 to 90, it means they are in the uppercase so convert it.
if (presentCharCode >= 65 && presentCharCode <= 90) { if (presentCharCode >= 65 && presentCharCode <= 90) {
// Convert the case by use of the above explanation. // Convert the case by use of the above explanation.
return String.fromCharCode(presentCharCode + 32) return String.fromCharCode(presentCharCode + 32)
@ -32,10 +32,10 @@ const titleCaseConversion = (inputString) => {
// Else return the characters without any modification. // Else return the characters without any modification.
return char return char
}) })
// return the first converted character and remaining character string. // Return the first converted character and remaining character string.
return firstChar + newWordChar.join('') return firstChar + newWordChar.join('')
}) })
// convert all words in a string and return it. // Convert all words in a string and return it.
return stringCollections.join(' ') return stringCollections.join(' ')
} }