Skip to content

Instantly share code, notes, and snippets.

@HashirHussain
Created January 1, 2024 14:32
Show Gist options
  • Save HashirHussain/75ea6ec5103440a2ba6845bf6087cf7a to your computer and use it in GitHub Desktop.
Save HashirHussain/75ea6ec5103440a2ba6845bf6087cf7a to your computer and use it in GitHub Desktop.
Complete the capitalizeFirstLetter below while being sure to capitalize the initial letter of each word in the provided string.
/**
Complete the capitalizeFirstLetter below while being sure to capitalize the initial letter of each word in the provided string.
e.g.
const str = "How can mirrors be real if our eyes aren't real"
str.capitalizeFirstLetter() //-> "How Can Mirrors Be Real If Our Eyes Aren't Real"
**/
String.prototype.capitalizeFirstLetter = function () {}
@antiproblemist
Copy link

antiproblemist commented Jan 2, 2024

String.prototype.capitalizeFirstLetter = function () {
	return this.split(" ").map(word => {
  	 return word.slice(0,1).toUpperCase() + word.slice(1)
  }).join(" ")
}

const str = "How can mirrors be real if our eyes aren't real"
const newStr = str.capitalizeFirstLetter() //-> "How Can Mirrors Be Real If Our Eyes Aren't Real"

console.log(newStr)

@HashirHussain
Copy link
Author

String.prototype.capitalizeFirstLetter = function () {
	return this.split(" ").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ")
}

const str = "How can mirrors be real if our eyes aren't real"
str.capitalizeFirstLetter() //-> "How Can Mirrors Be Real If Our Eyes Aren't Real"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment