Urgent! Need help!! Averaging local variables in global scope.

What I have is 4 variables that provide scores each stored locally in a function. The hard part is I need to now average these scores and store them in a global variable. I don’t know how to move the values of each local variable outside of its function.

*** each avg is stored locally in a function of its own.**

var finaltotal = (avg1+avg2+avg3+avg4);
var finalavg = Math.round(finaltotal / 4);

*** I need the average of these averages.**

I’m just unsure how to do this if it is at all possible. Thanks!

Hi, Daniel Braine .

You will need to return avg1 , avg2 , avg3 or avg4 from each of their individual functions to the above function/code.

If that doesn’t make sense, you will need to include the individual functions here so we can help you further.

@abergquist

$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);
var 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);
var 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);
var 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);
var avg4 = Math.round(s4totalCount / section4.length);
$w(“#text156”).text = avg4.toString();
}
})
})
*my attempt
// var finaltotal = (avg1+avg2+avg3+avg4);
// var finalavg = Math.round(finaltotal / 4);
//
// $w(“#text150”).text = finalavg.toString();
})

Hi, Daniel Braine .

Now that I see your code, I realize that each average is not being set within a specific function (for a total of 4 functions) but they’re all being set in 1 function.

It appears to be a scope issue; specifically, avg1 , avg2 , avg3 and avg4 are not in scope when you attempt to sum them.

Does replacing the following:

$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);
 var 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);
 var 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);
 var 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);
 var avg4 = Math.round(s4totalCount / section4.length);
            $w("#text156").text = avg4.toString();
            }
        })
    })

with the following:

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();
            }
        })

or with the following:

$w.onReady(function () {
 var avg1, avg2, avg3, avg4; 
 
 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();
            }
        })

resolve the issue for you?

Thanks for getting back to me @ abergquist ! I tried your previous example and for some reason it’s returning a ‘NaN’ which I think means it doesn’t know what kind of type to return. I tried setting “avg1=0, avg2=0 …” and it removes the NaN and gives 0, but it’s not picking up the averaged values… Not sure what to do because it still seems like the variables are still out of scope.

$w.onReady( function () {
var avg1, avg2, avg3, avg4;
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();
})

Hi, Daniel .

Does replacing the following:

 $w.onReady(function () {
  var avg1, avg2, avg3, avg4;

with the following:

  var avg1, avg2, avg3, avg4;
  $w.onReady(function () {

resolve the issue for you?

Hey @abergquist I tried moving the variables out of the function and I’m still getting a NaN instead of a number, could it be that It’s getting the values, only they aren’t the proper type?

Thanks for your help so far. Much appreciated.

NaN stands for Not a Number and indicates it’s undefined.

Does replacing the following code:

  var finaltotal = (avg1 + avg2 + avg3 + avg4); 

with the following code:

  var finaltotal = Number($w("#text57").text) + Number($w("#text153").text) + Number($w("#text121").text) + Number($w("#text156").text); 

resolve the issue for you?

Hi @abergquist thank you for bearing with me, unfortunately it’s still a NaN… wracking my brain :l