I'm trying to implement a simple Revrse Polish Notation calculator in Angular.js and so far the code can handle single-digit calculations like
2 3 +
How can I modify it to handle inputs with more than one digit, such as:
122 233 +
Its an idea that occurred to me after participating in this discussion). I suspect that I may have to use onkeypress and parseInt and/or parseFloat but am unsure. I look forward to your thoughts. Thank you.
JavaScript:
var calcApp = angular.module('calcApp', []);
calcApp.controller('calcCtrl', function ($scope) {
$scope.leftOperand = "";
$scope.operator = "";
$scope.rightOperand = "";
$scope.answer = "";
$scope.setOperand = function (operandEntered) {
($scope.leftOperand)
? $scope.rightOperand += operandEntered + " "
: $scope.leftOperand += operandEntered + " ";
console.log($scope.leftOperand,$scope.rightOperand);
};
$scope.setOperator = function (operatorEntered) {
$scope.operator = operatorEntered;
};
$scope.getAnswer = function () {
var result = $scope.leftOperand + $scope.operator + $scope.rightOperand;
var answer = eval(result);
if (answer % 1 !== 0){ // % 1 to identify floats
$scope.answer = answer.toFixed(2);
}
else {$scope.answer = answer;}
$scope.leftOperand = $scope.answer;
$scope.operator = "";
$scope.rightOperand = "";
};
$scope.setClear = function (a) {
$scope.leftOperand = "";
$scope.operator = "";
$scope.rightOperand = "";
$scope.answer = "";
};
});