Bryan Le Bryan Le - 6 months ago 9
Javascript Question

for loop and if statement, taking variable and storing it

I'm having an issue where a while loop is reading a record. I'm trying to compare the the entered value to the record to see if it's in there. When it does find a record, I want to store it in an empty array, then have it printed out.

for (counter = 0; counter < records.length; counter++) {
if(userEntered.value == records[counter]){
alert("help");
value = true;
comparedZipCode = records[counter];
}
}


.length
does not work because while-loop is reading a file and the for-loop is incrementing each time it reads the file and then comparing it to
userEntered
.

So it will have to be
records.readNextRecord()
for the if-statement

Answer

Just store it in comparedZipCode by declaring it as an array before you enter the loop.

var comparedZipCode = []
for (counter = 0; counter < records.length; counter++) {
    if(userEntered.value == records[counter]){
        alert("help");  
        value = true;
        comparedZipCode[counter] = records[counter];
    }
}