I am fairly new to Angular and Javascript so am in need of some guidance. I am wanting to work out the sum and average of values within an object array. The objects are pushed into the array through input boxes, here is my code so far:
var myApp = angular.module("myApp", []);
myApp.controller("myController", function($scope){
$scope.newLog = {};
$scope.logs = [
{project: "",
phase: "",
date: "",
startTime: "",
intTime: "",
endTime: "",
comments: ""}
];
$scope.saveLog = function(){
//CREATING DELTA TIME
var newTimeLog = $scope.newLog;
var begin = (newTimeLog.startTime).getTime();
var end = (newTimeLog.endTime).getTime();
var i = newTimeLog.intTime;
var ii = parseInt(i);
var intMilisec = ii*60000;
if( isNaN(begin) )
{
return "";
}
if (begin < end) {
var milisecDiff = end - begin;
}else{
var milisecDiff = begin - end;
}
var minusInt = milisecDiff - intMilisec;
var milisec = parseInt((minusInt%1000)/100)
, seconds = parseInt((minusInt/1000)%60)
, minutes = parseInt((minusInt/(1000*60))%60)
, hours = parseInt((minusInt/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
var deltaFormat = hours + " Hours " + minutes + " Minutes";
newTimeLog["deltaTime"] = deltaFormat;
$scope.logs.push($scope.newLog);
$scope.newLog = {};
};
$scope.intSum = function(){
var sum = 0;
for (var i = 0; i < $scope.logs.length; i++){
sum += $scope.logs[i].intTime;
}
return sum;
};
});
So the intSum
function is where I am having issues - I am wanting to sum the intTime
properties for all the objects. So if object1's intTime = 1
, object2's intTime = 2
, object3's intTime = 3
the intSum
should be 6
. However what I am getting from the IntSum
currently is 123
.
Any help would be greatly appreciated!