Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
First letter in a sentence dosn’t work in javascript (empty variable in string at a first place)

I use this (I guess typical) function to make a first letter in a sentece capital:

function firstLetteCap(string) {
return string.charAt(0).toUpperCase() + string.slice(1) }

Works perfect till there is an empty variable:

// This works perfect
first = "just"
sentence = `${first} a string`
document.getElementById("p2").innerHTML = `${firstLetteCap(sentence)}.`

// With an empty variable it doesn’t work (capitalized "a" expected)
first = ""
sentence = `${first} a string`
document.getElementById("p3").innerHTML = `${firstLetteCap(sentence)}.`

I would like to know how to fix that, but maybe some context would be also perfect (maybe I don’t get something on how the strings works with variables).

Expected: Just a string.

Thanks!

CodePen: https://codepen.io/vojtacejnek/pen/eYybRqg

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since, first letter of the sentence variable is (space).

first = ""
sentence = `${first} a string`
document.getElementById("p3").innerHTML = `${firstLetteCap(sentence)}.`

If you want to ignore the extra spaces, you can use .trim().

Refer: https://www.w3schools.com/java/ref_string_trim.asp

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda