Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.7K
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!