bo3m bo3m - 12 days ago 8
AngularJS Question

Custom Calculator get the last selected operation with a new value

I'm building a test calculator and i managed to get some functionalities working like Add, Multiply.

For the last part i need a way to get an Apply functionality that will perform the last operation with the newly selected number.

Examples of the calculator lifecycles

enter image description here

Here is a JSFiddle Link

Any ideas? Thank you!

HTML

<select ng-model="selectedOperation">
<option value="Add">Add</option>
<option value="Multiply">Multiply</option>
<option value="Apply">Apply</option>
</select>
<input ng-model="selectedNumber" />
<button ng-click="addItem()


JS

.controller('MyController', function ($scope) {
$scope.items = [];

$scope.addItem = function(){
$scope.items.push({
myOperation: $scope.selectedOperation,
myNumber: $scope.selectedNumber
});

$scope.selectedOperation = '';
$scope.selectedNumber = '';
}

$scope.AddToTotal = function(){
var total = 0;

for(var i=0; i<$scope.items.length; i++){
if($scope.items[i].myOperation ==="Add"){
total += $scope.items[i].myNumber;
}
if($scope.items[i].myOperation ==="Multiply"){
total *= $scope.items[i].myNumber;
}
if($scope.items[i].myOperation ==="Apply"){
??????????????
}
}
return total;
}
})

Answer

If I understand that example right, what you need is not performing the last operation with the newly selected number, but performing all the steps with the newly selected number. If so, then you have to create such variable startValue and update it when step with Apply value occurs.

angular.module('myApp', [])
    .controller('MyController', function ($scope) {
        $scope.items = [];
        $scope.startValue = 0;

        $scope.addItem = function(){
                if($scope.selectedOperation === 'Apply'){
                $scope.startValue = $scope.selectedNumber;
            }
            $scope.items.push({
                myOperation: $scope.selectedOperation,
                myNumber: $scope.selectedNumber 
            });

            $scope.selectedOperation = '';
            $scope.selectedNumber = '';
        }

        $scope.AddToTotal = function(){
            var total = $scope.startValue;

            for(var i=0; i<$scope.items.length; i++){
                if($scope.items[i].myOperation ==="Add"){
                    total += $scope.items[i].myNumber;
                }
                if($scope.items[i].myOperation ==="Multiply"){
                    total *= $scope.items[i].myNumber;
                }
                //if($scope.items[i].myOperation ==="Apply"){
                //    total = $scope.items[i].myNumber;
                //}
                //uita-te in array in ultimul item
            }
            return total;
        }

        $scope.resetItems = function(){
            console.log('reset')
            $scope.items.length = 0;
            $scope.startValue = 0;
        }
    })

And Demo:

http://jsfiddle.net/75xz5qvf/1/

Let me know if I'm right : )