User:Phlsph7/Readability.js: Difference between revisions

Content deleted Content added
add words per sentence
more consistent labels
 
(2 intermediate revisions by the same user not shown)
Line 185:
const sentenceElements = document.getElementsByClassName('sentence');
let totalSentenceCount = sentenceElements.length;
const sentenceLengthArray = [];
for(let sentenceElement of sentenceElements){
let sentenceText = getSentenceText(sentenceElement);
let words = getWords(sentenceText);
totalWordCount += words.length;
sentenceLengthArray.push(words.length);
for(let word of words){
let syllableCount = getSyllableCount(word);
Line 234 ⟶ 236:
addRow(overviewTableBody, 'Words', totalWordCount);
addRow(overviewTableBody, 'Syllables', totalSyllableCount);
addRow(overviewTableBody, 'Words perAverage sentence length', (totalWordCount/totalSentenceCount).toFixed(2));
addRow(overviewTableBody, 'Standard deviation<br>of sentence length', (getStandardDeviation(sentenceLengthArray)).toFixed(2));
function getStandardDeviation(numbers){
const sum = numbers.reduce(function(sum, number){return sum + number;});
const mean = sum / numbers.length;
const varianceSum = numbers.reduce(function(sum, number){console.log(Math.pow(number - mean, 2)); return sum + Math.pow(number - mean, 2);}, 0);
const variance = varianceSum/numbers.length;
const standardDeviation = Math.sqrt(variance);
return standardDeviation;
}
}