即使满足if语句,搜索函数也默认为else语句

时间:2015-11-09 22:37:11

标签: javascript object if-statement prompt

/ *包含美国州的大型数组,其缩写和州税额* /

var usStates = [
    { name: 'ALABAMA', abbreviation: 'AL', taxAmount: 5},
    { name: 'ALASKA', abbreviation: 'AK', taxAmount: 0},
    { name: 'ARIZONA', abbreviation: 'AZ', taxAmount: 4.54},
    { name: 'ARKANSAS', abbreviation: 'AR', taxAmount: 7.0},
    { name: 'CALIFORNIA', abbreviation: 'CA', taxAmount: 13.3},
    { name: 'COLORADO', abbreviation: 'CO', taxAmount: 4.63},
    { name: 'CONNECTICUT', abbreviation: 'CT', taxAmount: 6.7},
    { name: 'DELAWARE', abbreviation: 'DE', taxAmount: 6.75},
    { name: 'DISTRICT OF COLUMBIA', abbreviation: 'DC', taxAmount: 8.95},
    { name: 'FLORIDA', abbreviation: 'FL', taxAmount: 0 },
    { name: 'GEORGIA', abbreviation: 'GA', taxAmount: 6.0},
    { name: 'HAWAII', abbreviation: 'HI', taxAmount: 11.0},
    { name: 'IDAHO', abbreviation: 'ID', taxAmount: 7.4},
    { name: 'ILLINOIS', abbreviation: 'IL', taxAmount: 5.0},
    { name: 'INDIANA', abbreviation: 'IN', taxAmount: 3.4},
    { name: 'IOWA', abbreviation: 'IA', taxAmount: 8.98},
    { name: 'KANSAS', abbreviation: 'KS', taxAmount: 4.9},
    { name: 'KENTUCKY', abbreviation: 'KY', taxAmount: 6},
    { name: 'LOUISIANA', abbreviation: 'LA', taxAmount: 6},
    { name: 'MAINE', abbreviation: 'ME', taxAmount: 5.25},
    { name: 'MARYLAND', abbreviation: 'MD', taxAmount: 5.75},
    { name: 'MASSACHUSETTS', abbreviation: 'MA', taxAmount: 5.25},
    { name: 'MICHIGAN', abbreviation: 'MI', taxAmount: 4.25},
    { name: 'MINNESOTA', abbreviation: 'MN', taxAmount: 9.85},
    { name: 'MISSISSIPPI', abbreviation: 'MS', taxAmount: 4.25},
    { name: 'MISSOURI', abbreviation: 'MO', taxAmount: 6},
    { name: 'MONTANA', abbreviation: 'MT', taxAmount: 6.9},
    { name: 'NEBRASKA', abbreviation: 'NE', taxAmount: 6.84},
    { name: 'NEVADA', abbreviation: 'NV', taxAmount: 0},
    { name: 'NEW HAMPSHIRE', abbreviation: 'NH', taxAmount: 5},
    { name: 'NEW JERSEY', abbreviation: 'NJ', taxAmount: 8.97},
    { name: 'NEW MEXICO', abbreviation: 'NM', taxAmount: 4.9},
    { name: 'NEW YORK', abbreviation: 'NY', taxAmount: 8.82},
    { name: 'NORTH CAROLINA', abbreviation: 'NC', taxAmount: 7.75},
    { name: 'NORTH DAKOTA', abbreviation: 'ND', taxAmount: 3.99},
    { name: 'OHIO', abbreviation: 'OH', taxAmount: 5.925},
    { name: 'OKLAHOMA', abbreviation: 'OK', taxAmount: 5.25},
    { name: 'OREGON', abbreviation: 'OR', taxAmount: 9.9},
    { name: 'PENNSYLVANIA', abbreviation: 'PA', taxAmount: 3.07},
    { name: 'RHODE ISLAND', abbreviation: 'RI', taxAmount: 5.99},
    { name: 'SOUTH CAROLINA', abbreviation: 'SC', taxAmount: 7},
    { name: 'SOUTH DAKOTA', abbreviation: 'SD', taxAmount: 0},
    { name: 'TENNESSEE', abbreviation: 'TN', taxAmount: 6},
    { name: 'TEXAS', abbreviation: 'TX', taxAmount: 0},
    { name: 'UTAH', abbreviation: 'UT', taxAmount: 5},
    { name: 'VERMONT', abbreviation: 'VT', taxAmount: 8.95},
    { name: 'VIRGINIA', abbreviation: 'VA', taxAmount: 5.75},
    { name: 'WASHINGTON', abbreviation: 'WA', taxAmount: 0},
    { name: 'WEST VIRGINIA', abbreviation: 'WV', taxAmount: 6.5},
    { name: 'WISCONSIN', abbreviation: 'WI', taxAmount: 7.65},
    { name: 'WYOMING', abbreviation: 'WY', taxAmount: 0}
];

/ *从启动页面开始,询问用户是否要继续* /

var didUserConfirm = confirm("Welcome to Tip Calc" + "\n" + "Would you like to calculate this week's wages?");

var userState = [];

if (didUserConfirm === true) {userState.push(prompt("In which state are you employed?"));
                     }

/ *一个函数,用于搜索状态的州缩写和所得税金额,并将信息推送到userState数组* /

var searchStates = function(state){
    for (var key in usStates) {
        if(usStates[key].name === state.toUpperCase()){
            alert(state + " is a valid US State" + "\n" + "The state tax is " +     usStates[key].taxAmount);
           userState.push(usStates[key].name, usStates[key].taxAmount, usStates[key].abbreviation);

         }

/ *即使前一个if语句已满足,这也是默认的else语句。 * /

        else {
            alert(state + "is not a valid US State. Please try again.")
        };
    }
};
searchStates(userState[0]);
console.log(userStateInfo[1]);

1 个答案:

答案 0 :(得分:0)

看起来您正在尝试识别第一个匹配状态,并且仅在未找到状态时执行“其他”条件:

var searchStates = function(state){
    for (var key in usStates) {
        if(usStates[key].name === state.toUpperCase()){
            alert(state + " is a valid US State" + "\n" + "The state tax is " +     usStates[key].taxAmount);
           userState.push(usStates[key].name, usStates[key].taxAmount, usStates[key].abbreviation);
            return; // stop once we've found a state
         }
    }
    // if we haven't stopped, that means we didn't find one
    alert(state + "is not a valid US State. Please try again.")

};