for loop和if语句,获取变量并存储它

时间:2016-05-08 20:56:17

标签: javascript if-statement for-loop nested-loops

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

我无法获取用户输入的值,现在将其与记录进行比较。

它搜索邮政编码,但没有什么可比较的。也许.value不应该在那里?

这是整个代码:

       records = openZipCodeStudyRecordSet();




    userEntered = prompt("Search students in current zipcode:");

    userEntered = Number(userEntered);


while (records.readNextRecord()) {


    firstName   = records.getSampleFirstName();
    lastName    = records.getSampleLastName();
    gender      = records.getSampleGender();
    zipCode     = records.getSampleZipCode();


    for (counter = 0; counter < records.readNextRecord(zipCode); counter++) {

        if(userEntered.value == records.readNextRecord[counter]){

            value = true;

            comparedZipCode[counter] += records.readNextRecord[counter];


            }

    }

}
    //test feature
    document.write(comparedZipCode);

1 个答案:

答案 0 :(得分:1)

通过在进入循环之前将其声明为数组,将其存储在comparisonZipCode中。

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