SO Caculator Example
JavaScript
var $scope = {
operator: null,
answer: null,
leftOperand: null,
rightOperand: null,
};
$scope.setOperand = function (operand) {
(function(prop){
$scope[prop] = +$scope[prop] + operand
})
(['leftOperand','rightOperand'].reduce(function(t, k){
console.log(t, $scope[t], k, $scope[k]);
return !+$scope[k] ? k : t;
}));
};
$scope.setOperand(6);
$scope.setOperand(6);
$scope.operator = '-';
$scope.setOperand(6);
$scope.setOperand(6);
console.log($scope);