# To Upper Case in 30 Seconds— JavaScript

Let’s jump right into it. The common “Upper Case the first letter of each sentence” or “Change every nth element to Upper-Case” Algorithm (it is an Algorithm right); So “easy” yet over 50% of people fail the question; its the simplicity that throws people off, and for anyone with experience in JavaScript, the .toUpperCase() method acts strange sometimes; We will dissect the problem below.

So, let’s step through this,

# What do we need to do?

Write out an Algorithm that takes in a string of words and capitalizes the first letter of each word. There are no special characters, all letters are lowercase, there are no numbers.

So, we need to write a function that takes in a string.

`function toUpperCaseFunc(str){}`

We know that we will need to capitalize the first letter of each word, so we are going to “split” each word into an array of words where there are spaces.

`const arrayOfWords = str.split(" ")`

We will have to loop through this array, and grab the first letter of each word and capitalize it.

`for( let i = 0; i < arrayOfWords.length; i++){let array = arrayOfWords[i] word.push(array.charAt(0).toUpperCase() + array[i].slice(1))}`

We will need push the mutated word into an array, so we will need to instantiate an empty array.

`let word = [];for( let i = 0; i < arrayOfWords.length; i++){let array = arrayOfWords[i] word.push(array.charAt(0).toUpperCase() + array[i].slice(1))}`

and after looping and pushing we will have to “join” our arrays back into a sentence.

`function(str){let arrayOfWords = str.split(" ")let word = [];for( let i = 0; i < arrayOfWords.length; i++){let array = arrayOfWords[i] word.push(array.charAt(0).toUpperCase() + array[i].slice(1))}return word.join(" ")}`

There are a million different ways to accomplish this feat. the main issue most people run into is the proper use of toUpperCase(), toUpperCase when used with a string returns a copy of the element upperCased, but because JavaScript Strings are immutable is isn’t able to change the actual value of the string outside of the immediate area it is placed, so if you try to assign array[i].toUppercase to a variable, it will return in its original state when you try to read it without the method attached.

That is why we had to push, toUpperCase, concatenate, and slice, all in one motion.

There are easy alterations to this function that will allow for more complex implementations; If you have any questions, or a better or different way to accomplish this, post or comment below;