Can anyone help me understand why avg1,avg2,avg3,avg4 from each section when averaged
at the bottom of the code produces a NaN?? Instead of a numeric value?
Here is my code, thanks!!
*each section gives an averaged score I'm trying to average each section now to produce over-all score.
var avg1, avg2, avg3, avg4;
$w.onReady(function () {
const section1 = [$w("#radioGroup1"), $w("#radioGroup2"), $w("#radioGroup3"), $w("#radioGroup4")];
let s1totalCount = 0;
section1.forEach(e => {
e.onChange((event) => {
let selected = section1.filter(o => o.valid);
s1totalCount = selected.reduce((a, c) => a + Number(c.value), 0);
avg1 = Math.round(s1totalCount / section1.length);
$w("#text57").text = avg1.toString();
})
})
const section2 = [$w("#radioGroup5"), $w("#radioGroup6"), $w("#radioGroup7"), $w("#radioGroup8")];
let s2totalCount = 0; section2.forEach(e => {
e.onChange((event) => {
let selected = section2.filter(o => o.valid);
s2totalCount = selected.reduce((a, c) => a + Number(c.value), 0);
avg2 = Math.round(s2totalCount / section2.length);
$w("#text153").text = avg2.toString();
})
})
const section3 = [$w("#radioGroup9"), $w("#radioGroup10"), $w("#radioGroup11"), $w("#radioGroup12")];
let s3totalCount = 0; section3.forEach(e => {
e.onChange((event) => {
let selected = section3.filter(o => o.valid);
s3totalCount = selected.reduce((a, c) => a + Number(c.value), 0);
avg3 = Math.round(s3totalCount / section3.length);
$w("#text121").text = avg3.toString();
})
})
const section4 = [$w("#radioGroup13"), $w("#radioGroup14"), $w("#radioGroup15"), $w("#radioGroup16")];
var s4totalCount = 0; section4.forEach(e => {
e.onChange((event) => {
var selected = section4.filter(o => o.valid);
s4totalCount = selected.reduce((a, c) => a + Number(c.value), 0);
avg4 = Math.round(s4totalCount / section4.length);
$w("#text156").text = avg4.toString();
})
})
var finaltotal = (avg1 + avg2 + avg3 + avg4);
var finalavg = Math.round(finaltotal / 4);
$w("#text150").text = finalavg.toString();
})
Thanks @givemeawhisky any suggestions on how I can get the average without it returning NaN? Thanks heaps!!
NaN means not a number, basically you are trying to use a number that is not acceptable, not a legal number.