Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda