Sentence Capitalization-v1

Sentence Capitalization v1


Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.


Examples
capitalize(‘a short sentence’) => return ‘A Short Sentence
capitalize(‘a lazy fox’) => return ‘A Lazy Fox’
capitalize(‘look, it is working!’) => return ‘Look, It Is Working’

function capitalize(str){

	const words = [];
	for(let word of str.split(' ')){
		words.push(word[0].toUpperCase() + word.splice(1))
	}

	return words.join(' ')
}



First we create function anagrams with parameters stringA and stringB.

Then we create helper function cleanString with parameter str and we return str.replace which is build in JavaScript method. replace will strip out any spaces or any punctuation and turn into lowercase with chaining the toLowerCase();

Than we chain .split(‘’) method to turn the string str into array

Then we chain .sort() method to sort alphabetically the characters from the array done with split method

In the end we join the characters from the array, sorted with sort method and now joined with .join(‘’) method. All these functions are build in JavaScript helpers

From the cleanString function all these tasks done above are returned

In the main function anagrams we return cleanString(stringA) === cleanString(stringB) and if it is true it is anagram.

comments powered by Disqus