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

219
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'toUpperCase')

I have been trying to convert the text input to proper case and sentence case. Proper case seems to work fine but the sentence case throws Uncaught TypeError: Cannot read properties of undefined (reading 'toUpperCase'). Went through this post and applied few changes but found no luck. Surprisingly, the .toUpperCase() works just fine in the proper case function.

document.getElementById("proper-case").addEventListener("click", function() {
  let words = document.querySelector("textarea").value.split(" ");
  let string = "";
  for (let i = 0; i < words.length; i++) {
    string += words[i][0].toUpperCase() + words[i].substring(1).toLowerCase();
    if (i != words.length - 1) {
      string += " ";
    }
  }
  document.querySelector("textarea").value = string;
});


document.getElementById("sentence-case").addEventListener("click", function() {
  let sentences = document.querySelector("textarea").value.split(".");
  let string = "";
  console.log(sentences);
  for (let i = 0; i <= sentences.length - 1; i++) {
    if (i == 0) {
      string += sentences[i][0].toUpperCase() + sentences[i].substring(1).toLowerCase() + ".";
    } else {
      string += " " + sentences[i][1].toUpperCase() + sentences[i].substring(2).toLowerCase() + ".";
    }
  }
  console.log(string)
  document.querySelector("textarea").value = string;
})
<textarea></textarea>

<button id="proper-case">Proper Case</button>
<button id="sentence-case">Sentence Case</button>

Can someone please tell me what the problem here is?

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

0

The .split('.') operation returns an empty string at its last array item, therefore, sentences[i][1] is undefined. To solve this, you can check if sentences[i] is not an empty string.

In addition, you can use String.trimLeft() on the else case.

about 4 years ago · Juan Pablo Isaza Relatório

0

This line causes the error.

string += " " + sentences[i][1].toUpperCase() + sentences[i].substring(2).toLowerCase() + ".";

your split separates the string at a point. if there is nothing after the point, there is also an empty element in the array. and that is why the [0] throws an error. because a lerrer string has no first character. to fix the error, you must first catch this case.

for example

document.getElementById("sentence-case").addEventListener("click", function() {
  let sentences = document.querySelector("textarea").value.split(".");
  let string = "";
  console.log('=>', sentences);
  for (let i = 0; i <= sentences.length - 1; i++) {
    if (i == 0) {
      string += sentences[i][0].toUpperCase() + sentences[i].substring(1).toLowerCase() + ".";
    } else {
      console.log(sentences[i][1]);
      let s1 = ''      
      let s3 = ''      
      if(typeof sentences[i][0] !== "undefined") {        
        s1 = " " + sentences[i][0].toUpperCase();
        s3 = ".";
      } 
      string += s1 + sentences[i].substring(2).toLowerCase() + s3;
    }
  }
  console.log(string)
  document.querySelector("textarea").value = string;
})
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