Search This Blog

2026/08/11

Javascript:Capitalize first and last character in sentence

 



Capitalize first and last character in sentence.

Code:

let sentence = "hello john how do you do"

function firstCharCapital(sentence){
  return sentence.at(0).toUpperCase() + sentence.slice(1)
}

function lastCharCapital(sentence){
   return  sentence.slice(0,sentence.length -1 ) +  sentence.at(sentence.length -1).toUpperCase()
}

let firstCharCapitalized = firstCharCapital(sentence)
console.log(firstCharCapitalized)


let lastCharCapitalized = lastCharCapital(sentence)
console.log(lastCharCapitalized)

Output:
Hello john how do you do
hello john how do you dO

Explanation:I have written two functions to get desired result we can call them one
on other like firstCharCapital(lastCharCapital(sentence))

No comments:

Post a Comment