Skip to content

Instantly share code, notes, and snippets.

@Smakar20
Created January 23, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Smakar20/6a1ad2403b568845780dcb694adc93ab to your computer and use it in GitHub Desktop.
Save Smakar20/6a1ad2403b568845780dcb694adc93ab to your computer and use it in GitHub Desktop.
capitalWords.js created by smakar20 - https://repl.it/@smakar20/capitalWordsjs
/* In the English language there are many grammatical uses of capitalisation, where the first letter of a word is rendered in uppercase. For example, proper nouns and the beginning of sentences. Therefore, when analysing a block of text, we may be interested in capitalised words.
In this Kata from an input string we want to return an array of the form [[Capitalised Words],number] where [Capitalised Words] is the array consisting of all words beginning with a single uppercase letter and number is its count (size).
The input string will only contain alphabetic chars and punctuation, no numerals or double whitespace for example.
So:
"This is an Input string Example." => [["This","Input","Example"],3]
"Do not count IBM Acronyms or other CAPITAL words." => [["Do","Acronyms"],2]
"ROFLMAO" => [[],0]
"The: Greatest is I." => [["The","Greatest","I"],3]
"who needs grammar and stuff i only do lowercase" => [[],0]
"One" => [["One"],1]
"P. T. Barnum" => [["P","T","Barnum"],3])
"one" => [[],0]
*/
function capitalWords(str){
//happy days!
if (str.trim() === '') return [[], 0];
var inpArr = str.replace(/[^\w\s]/gi, '').split(' ');
var outWrdArr = [];
for (var i = 0; i < inpArr.length; i++) {
if (/^[A-Z]*$/.test(inpArr[i][0])) outWrdArr.push(inpArr[i]);
}
return [outWrdArr, outWrdArr.length];
}
capitalWords("P. T. Barnum"); // [["P","T","Barnum"],3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment