How to calculate reading time with JavaScript

I always see these "x min read" reading time on articles I see within the net.

Here's a quick function on how you can do these too!

function calculateReadingTime() {
    // Gets the article class container containing the content.
    const content = document.getElementById("logs-container").innerText;

    // Average adult words per minute.
    const avgWpm = 238;

    // Count the words for the content
    const words = content.trim().split(/\s+/).length;

    // Calculate total time takes to read the content based on the average words per minute of an adult.
    const totalTime = Math.ceil(words / avgWpm);

    document.getElementById("time-container").innerText =
        totalTime + " min read";
}

calculateReadingTime();

This function gets the all the characters within your content container, which you'll need to count how many words there are in the content.

After that you'll have to divide the content based on the average words per minute of an adult (based it on this study). And that should be it!