JavaScript Algorithm: Camelcase a Sentence

JavaScript Algorithm: Camelcase a Sentence

Create a function that will capitalize the first letter of each word in a text.

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

The goal of the function is to output the same string but with every word in the string capitalized or camel-cased.


captializeWords("Create a function that will capitalize the first letter of each word in a text");

//output: "Create A Function That Will Capitalize The First Letter Of Each Word In A Text"

The first thing we are going to do is create a variable called newWord. This variable will contain our new camel-cased string.

We are going to use a combination of the replace() method and regular expression.

let newWord = text.replace(/(\b\w)/g, function(str){      
    return str.toUpperCase();    

Our regular expression, /(\b\w)/g, will match every first character in a word boundary. In other words, it will match the first character of each word. Inside our callback function, for every character the regular expression matches, we replace that character with a capitalized character.

After the function capitalizes every word in the string, we return our new string.

return newWord;

javascript algorithms coding

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

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

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.

What is JavaScript - Stackfindover - Blog

What is PHP: - Who invented PHP, how it works, answers to all such questions about PHP, and much other information, you are going to