David

Calculating Read time in JavaScript

🍿 2 min read • October 8, 2020

This is a very simple task. First, we need a sentence to try this out. Let's use "The quick brown fox jumps over the lazy dog". A few thing we need to know is:

So, we have to split the text every 5 letters and divide it by 225 WPM. This can easily be done with JavaScript:

"The quick brown fox jumps over the lazy dog.".match(/.{1,5}/g).length/225;

Which should return a value of 0.04. So are we done? No. We also need make it human readable. First we need to round to the nearest whole number by using Math.round() :

Math.round("The quick brown fox jumps over the lazy dog.".match(/.{1,5}/g).length/225);

Which should return 0. Now we are almost done. Let's make it human readable. We can give it summaries using if/else statements.

Lets use:

as statements.

which returns Less than a minute read.

Let's make it ready for "production" by wrapping it around a function and compressing it using JScompress. I put the source code on [P2Pbin](https://dev.to/aboutdavid/p2pbin-a-peer-to-peer-paste-sharing-site-with-no-servers-2f8c), which you can find [here.](https://p2pbin.glitch.me/?id=QmSUskuMStqUSb4huDVDWc94UPE3v5AyX2aNp4nUoWw9tp)