Skip to content

Instantly share code, notes, and snippets.

@SimonBrandner
Created February 20, 2022 12:07
Show Gist options
  • Save SimonBrandner/1e46eefa5a66467c2076456fbc24af95 to your computer and use it in GitHub Desktop.
Save SimonBrandner/1e46eefa5a66467c2076456fbc24af95 to your computer and use it in GitHub Desktop.
Text stats
const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
let currentSentence = "";
let longestSentence = "";
let numberOfSentences = 0;
let numberOfWords = 0;
const numberOfLettersInSentence = (sentence) => Array.from(sentence).reduce((acc, curr) => {
if (curr !== " ") return acc + 1;
return acc;
}, 0);
const calculate = () => {
for (const character of text) {
currentSentence += character;
if (character === " ") numberOfWords++;
if (character !== ".") continue;
if (numberOfLettersInSentence(currentSentence) > numberOfLettersInSentence(longestSentence)) longestSentence = currentSentence;
numberOfSentences++;
currentSentence = "";
}
}
calculate();
console.log("Longest sentence:", longestSentence)
console.log("Avarage sentence length:", numberOfWords / numberOfSentences)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment