From 4810119b447eb2d18cd4edb44368fb71ba797f93 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sat, 28 Aug 2021 12:54:14 +0530 Subject: [PATCH 1/3] add TitleCaseConversion method --- Conversions/TitleCaseConversion.js | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Conversions/TitleCaseConversion.js diff --git a/Conversions/TitleCaseConversion.js b/Conversions/TitleCaseConversion.js new file mode 100644 index 000000000..0a3140596 --- /dev/null +++ b/Conversions/TitleCaseConversion.js @@ -0,0 +1,33 @@ +const TitleCaseConversion = (inputString) => { + // Extact all space seprated string. + const stringCollections = inputString.split(' ').map(word => { + let firstChar = ''; + // Get a character code by the use charCodeAt method. + 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(firstCharCode >= 97 && firstCharCode <= 122){ + // Convert the case by use of the above explanation. + firstChar += String.fromCharCode(firstCharCode - 32); + }else{ + // Else store the characters without any modification. + firstChar += word[0]; + } + const newWordChar = word.slice(1).split('').map(char => { + // Get a character code by the use charCodeAt method. + 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 (presentCharCode >= 65 && presentCharCode <= 90) { + // Convert the case by use of the above explanation. + return String.fromCharCode(presentCharCode + 32) + } + // Else return the characters without any modification. + return char + }); + // return the first converted character and remaining character string. + return firstChar + newWordChar.join(''); + }); + // convert all words in a string and return it. + return stringCollections.join(' '); +} + +console.log(TitleCaseConversion("thIs is A mY best n string.")); \ No newline at end of file From da675fb23792ede85e420a90e6c15984982ea2e4 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sat, 28 Aug 2021 12:58:37 +0530 Subject: [PATCH 2/3] add Problem statement and Explanation source in TitleCaseConversion --- Conversions/TitleCaseConversion.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Conversions/TitleCaseConversion.js b/Conversions/TitleCaseConversion.js index 0a3140596..ec5c95394 100644 --- a/Conversions/TitleCaseConversion.js +++ b/Conversions/TitleCaseConversion.js @@ -1,3 +1,12 @@ +/* + Problem statement and Explanation : https://www.codeproject.com/Tips/162540/Letter-Case-Conversion-Algorithms-Title-Case-Toggl +*/ + +/** + * The TitleCaseConversion converts a string into a title case string. + * @param {String} inputString input string + * @returns {String} + */ const TitleCaseConversion = (inputString) => { // Extact all space seprated string. const stringCollections = inputString.split(' ').map(word => { @@ -30,4 +39,4 @@ const TitleCaseConversion = (inputString) => { return stringCollections.join(' '); } -console.log(TitleCaseConversion("thIs is A mY best n string.")); \ No newline at end of file +module.exports = TitleCaseConversion; \ No newline at end of file From 705b1236c86ebd1f0d6f5f8986e52bab83485661 Mon Sep 17 00:00:00 2001 From: Suryapratap Singh Date: Sat, 28 Aug 2021 13:00:43 +0530 Subject: [PATCH 3/3] re-formate TitleCaseConversion method with standard.js --- Conversions/TitleCaseConversion.js | 60 +++++++++++++++--------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/Conversions/TitleCaseConversion.js b/Conversions/TitleCaseConversion.js index ec5c95394..2db9620ea 100644 --- a/Conversions/TitleCaseConversion.js +++ b/Conversions/TitleCaseConversion.js @@ -8,35 +8,35 @@ * @returns {String} */ const TitleCaseConversion = (inputString) => { - // Extact all space seprated string. - const stringCollections = inputString.split(' ').map(word => { - let firstChar = ''; - // Get a character code by the use charCodeAt method. - 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(firstCharCode >= 97 && firstCharCode <= 122){ - // Convert the case by use of the above explanation. - firstChar += String.fromCharCode(firstCharCode - 32); - }else{ - // Else store the characters without any modification. - firstChar += word[0]; - } - const newWordChar = word.slice(1).split('').map(char => { - // Get a character code by the use charCodeAt method. - 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 (presentCharCode >= 65 && presentCharCode <= 90) { - // Convert the case by use of the above explanation. - return String.fromCharCode(presentCharCode + 32) - } - // Else return the characters without any modification. - return char - }); - // return the first converted character and remaining character string. - return firstChar + newWordChar.join(''); - }); - // convert all words in a string and return it. - return stringCollections.join(' '); + // Extact all space seprated string. + const stringCollections = inputString.split(' ').map(word => { + let firstChar = '' + // Get a character code by the use charCodeAt method. + 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 (firstCharCode >= 97 && firstCharCode <= 122) { + // Convert the case by use of the above explanation. + firstChar += String.fromCharCode(firstCharCode - 32) + } else { + // Else store the characters without any modification. + firstChar += word[0] + } + const newWordChar = word.slice(1).split('').map(char => { + // Get a character code by the use charCodeAt method. + 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 (presentCharCode >= 65 && presentCharCode <= 90) { + // Convert the case by use of the above explanation. + return String.fromCharCode(presentCharCode + 32) + } + // Else return the characters without any modification. + return char + }) + // return the first converted character and remaining character string. + return firstChar + newWordChar.join('') + }) + // convert all words in a string and return it. + return stringCollections.join(' ') } -module.exports = TitleCaseConversion; \ No newline at end of file +module.exports = TitleCaseConversion