As someone who is relatively new to Angular and Javascript, I am reaching out for some assistance. My goal is to calculate the sum and average of values within an object array. The objects are added to the array via input boxes, and here is the code I have developed 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(){
//CALCULATING 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;
};
});
The issue lies in the intSum
function - where I aim to sum up the intTime
properties of all the objects. For instance, if object1's intTime = 1
, object2's intTime = 2
, and object3's intTime = 3
, the expected result from intSum
should be 6
. However, the current output from intSum
is 123
.
Any guidance on this matter would be highly appreciated!