Home > Net >  why isnt javascript 'For' statement working in array?
why isnt javascript 'For' statement working in array?

Time:06-16

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.

CodePudding user response:

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);
  • Related