Introduction
This week, I implemented a feature that shows you how much time you have to spend to read a blog article.
Background
In the first step, I used my model BlogStory
to get the content.
Using the Code
- It counts the spaces between the words and adds 1. So now, we know how many words we have in that article.
- Most people can read 200 to 250 in one minute. So we need to divide the counted words by 250. Then we know how many minutes it will take to read.
- Then we combine a modulo with a divide to get the seconds.
@{
var word_count = @Model.Body;
var counts = word_count.Count(ch => ch == ' ') + 1;
var minutes = counts / 250;
var seconds = counts % 250 / (250 / 60);
var str_minutes = (minutes == 1) ? "Minute " : "Minutes ";
var str_seconds = (seconds == 1) ? "Second " : "Seconds ";
}
Now, we place the code for displaying the estimated time to read:
<i class="fas fa-clock"></i> @minutes @str_minutes @seconds @str_seconds
On the place where this snippet is, will be the estimated time to read.
History
- 7th May, 2019: Initial version