0
I’m making a code to get the average values of a list, however the result is being 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 sum 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?