CMU Coding Bootcamp
at main 9 lines 573 B view raw
1// Export a function called calculateReadTime that takes content as input. 2export function calculateReadTime(content) { 3 // Define the average reading speed in words per minute. 4 const wordsPerMinute = 200; 5 // Calculate the number of words in the content by trimming whitespace and splitting the string into an array of words. 6 const wordCount = content.trim().split(/\s+/).length; 7 // Calculate the read time in minutes by dividing the word count by the words per minute and rounding up to the nearest whole number. 8 return Math.ceil(wordCount / wordsPerMinute); 9}