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

1.7K
Vistas
why isnt javascript 'For' statement working in array?

i have a script that creates an array of words from a sencence and then capitalises each letter i am getting sintax errors whenever i run the script. Here is all of the JavaScript that is related to the input and output boxes.

function validateForm() {
    var addressInput = regForm.addressBox.value;
    var postCode = regForm.postCode.value;
    var townInput = regForm.townBox.value;
    var addressArray = addressInput.split(" ");
    var townArray = townInput.split(" ");

    for (let i = 0; i < addressArray.length; i++) {
        var addressArray[i] = addressArray[i][0].toUpperCase() + addressArray[i].substring(1);
    }
    var addressCap = addressArray.join(" ");
    
    for (let i = 0; i < townArray.length; i++) {
        var townArray[i] = townArray[i][0].toUpperCase() + townArray[i].substring(1);
    }
    var townCap = townArray.join(" ");
    
    if (addressCap > 1 ) {
        if (townCap > 1) {
            if (postCode == 4) {
                document.getElementById('addressOutput').innerHTML = "Your address is: " + addressCap + ", " + townInput + ", " + postCode;
            }
            if (postCode != 4) {
                document.getElementById('addressOutput').innerHTML = "Please fill out the post code box";
            }
        }
        if (townCap < 1) {
            document.getElementById('addressOutput').innerHTML = "Please fill out the town box";
        }
    }
    if (addressCap < 1) {
        document.getElementById('addressOutput').innerHTML = "Please check your address for errors";
    }
}

the error is for the i in addressArray[i] and the townArray[i] i feel like it is stupidly simple but im stumped.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

var addressArray[i] = isn't valid syntax.

Drop the var, as you're not declaring a variable:

addressArray[i] = addressArray[i][0].toUpperCase() + addressArray[i].substring(1);
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