Last active
March 9, 2024 14:10
-
-
Save pazguille/0488168436a4badf5e734cd0f44af2ce to your computer and use it in GitHub Desktop.
Calculate the estimated reading time of an article using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readingTime(txt) { | |
const wpm = 225; // average adult reading speed (words per minute). | |
const words = txt.trim().split(/\s+/).length; | |
const time = Math.ceil(words / wpm); | |
return time; | |
} | |
const time = readingTime( | |
document.querySelector("article").innerText | |
); | |
console.log(time + ' min read'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment