Skip to content

Commit dbf4715

Browse files
committed
readme file updated
1 parent 2d64d33 commit dbf4715

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,40 @@ __Code Implementation:__
321321
1. Using .forEach Method:
322322
The .forEach method in javascript runs a provided function on each element within array
323323

324+
```js
325+
function sentenceCap(text) {
326+
let wordArray = text.toLowerCase().split(' ')
327+
328+
let capsarray = []
329+
330+
wordArray.forEach(word => {
331+
capsarray.push(word[0].toUpperCase()+ word.slice(1) )
332+
});
333+
334+
return capsarray.join(' ')
335+
336+
}
337+
console.log(sentenceCap("ARTIFICIAL"))
338+
//will return Artificial
339+
```
340+
341+
<p>
342+
343+
* We call the .toLowerCase() method on the string of text received to convert the entire sentence to lowercase. We also chain the .split() method in sequence to divide the lowercase sentence into an array of words as shown below. This array is stored as wordsArray
344+
</p>
345+
<p>
346+
347+
* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
348+
* The function takes the first letter of the word and turns it to uppercase using the .toUpperCase() method. To retrieve the remaining part of the word in lowercase, we use the .slice() method to slice the string starting from position 1 till the end.
349+
350+
* We combine the transformed first letter and the sliced section together to form the capitalized word which we push into our array of capitalized words capsArray.
351+
352+
* After this process has being carried out for every word, capsArray now holds the capitalized version of every word in the sentence
353+
354+
* Finally .join() method is used
355+
</p>
356+
357+
324358
2. Using .map and .slice method:
325359
The .map method is used to create a new array with the results gotten from calling a provided function on every element in the array on which it is called.
326360

0 commit comments

Comments
 (0)