Skip to content

Instantly share code, notes, and snippets.

@pazguille
Last active March 9, 2024 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pazguille/0488168436a4badf5e734cd0f44af2ce to your computer and use it in GitHub Desktop.
Save pazguille/0488168436a4badf5e734cd0f44af2ce to your computer and use it in GitHub Desktop.
Calculate the estimated reading time of an article using JavaScript
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