简体   繁体   中英

How to write if ,else block for multiple conditions?

I am new to javascript what is right way to write if and else block with below code i am getting an error on else condition that syntax is not correct. Please let me know what i have implemented wrong.

main.js

if (dataRow.opriskYesNo === 'Yes') {
    $scope.opRiskCompleted = 'Y';
} else if (dataRow.opriskYesNo === 'No') {
    $scope.opRiskCompleted = 'N';
} else(dataRow.opriskYesNo === '') {
    $scope.opRiskCompleted = '';
    $scope.opRiskCompleted = '';
}

You should have else without any condition in your last conditional code block.

Though the improved version would look like below, I'd say maintain one object with key value and use it.

var mappedModel = {'Yes': 'Y', 'No': 'N'};
$scope.opRiskCompleted = mappedModel[dataRow.opriskYesNo] || '';

Why don't you use a Switch?

switch(dataRow.opriskYesNo){
case "Yes":
    $scope.opRiskCompleted = 'Y';
    break;
case "No":
     $scope.opRiskCompleted = 'N';
    break;
default:
     $scope.opRiskCompleted = '';
    break; }

Try this:

if(dataRow.opriskYesNo==='Yes'){
    $scope.opRiskCompleted = 'Y';
} else if (dataRow.opriskYesNo==='No') {
    $scope.opRiskCompleted = 'N';
} else {
    $scope.opRiskCompleted = '';
    $scope.opRiskCompleted = '';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM