I am making a code to get the average values of a list, however the result is NaN (not a number).
import { SimpleTableAnalysis } from '../model/SimpleTableAnalysis'
import { UserInfo } from '../model/UserInfo';
export class MeanAnalysis implements SimpleTableAnalysis {
public analysis(userInfoList: Array<UserInfo>): number{
var sum: number = 0.0;
userInfoList.forEach(userInfo => sum += userInfo.getCredit() );
return sum/(userInfoList.length);
}
}
The error happens inside the forEach, the code recognizes the value of the method 'getCredit ()', which returns number, and it is possible to perform sum in it, however with the variable 'sum' (external variable to forEach) returns me NaN even in a normal for.
Follow the conversion to javascript:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: !0
});
var MeanAnalysis = function() {
function e() {}
return e.prototype.analysis = function(e) {
var n = 0;
return e.forEach(function(e) {
return n += e.getCredit()
}), n / e.length
}, e
}();
exports.MeanAnalysis = MeanAnalysis;
How do I fix this?