JavaScript Algorithm: Word Count

JavaScript Algorithm: Word Count

Create a function that will return the number of words in a text.

We are going to write a function called countWords that will accept a string, text, as an argument.

The goal of the function is to return how many words are in that string of text.


countWords("JavaScript!!!    ");
// output: 1

countWords("I had 3 slices of bread");
// output: 6
countWords("    JavaScript is cool   Core   ");
// output: 4
countWords("I like to learn JavaScript with codeguppy");
// output: 7

The function should ignore any non-alphanumeric characters as part of the word count. Words containing hyphens should also be counted as one word.

The first thing we do is create a variable called removeChar. In this variable, we remove any excess whitespace within the text and punctuation. For this, we use a combination of regular expression and the replace() method.

let removeChar = text.replace(/[^A-Za-z]\s+/g, " ");

The regular expression inside the replace() method, /[^A-Za-z]\s+/g, takes all non-alphanumeric characters and replaces them with single spaces. In detail:

/[regular expression]/g: Instead of stopping at the first match, the g looks at all the possible matches in the text.

[^A-Za-z]: Matches any character that isn’t A-Z and a-z.

\s: Matches whitespace characters.

+: Matches 1 or more of the preceding token (in this case above, the whitespace).

With all those characters removed, it’ll be easier to count the number of words in text.

javascript algorithms coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Coding Interviews: Common Algorithms

I previously wrote about some common coding algorithms that you’ll run into on your job search. The article was received fairly well so I thought I’d do a couple more to help in your journey to becoming more familiar with algorithms.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Coding Interviews: Common Algorithms

Today I am going to be sharing two common coding algorithms as well as their solutions and some links to resources that will help you expand your knowledge of algorithms.

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.