1

And I don't understand why. I'm suspecting it is a namespace issue but the function and its calls are defined within the same doc ready function.

When I try to call the function in the console I get calcPCs is not defined

$(document).ready(function() {
    function calcPCs(id) {

        var si = $(id).find(".Screened-In").text();
        var so = $(id).find(".Screened-Out").text();

        var ref = $(id).find(".Referred").text();
        var ret = $(id).find(".Retained").text();

        $(id).find(".Screened-In").append(' (' + Math.floor((+si / (+si + +so)) * 100) + '%)');
        $(id).find(".Screened-Out").append(' (' + Math.floor((+so / (+si + +so)) * 100) + '%)');

        $(id).find(".Referred").append(' (' + Math.floor((+ref / (+ref + +ret)) * 100) + '%)');
        $(id).find(".Retained").append(' (' + Math.floor((+ret / (+ref + +ret)) * 100) + '%)');
    };            

    calcPCs("#northwest");
    calcPCs("#northeast");
    calcPCs("#west");
    calcPCs("#east");
    calcPCs("#central");
    calcPCs("#gtr");
});
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
user1765369
  • 1,333
  • 3
  • 11
  • 19

1 Answers1

1

You can't call calcPCs from the console. Any code you type in the console is executed in the global namespace, but calcPCs was defined inside the anonymous function you passed to .ready(), and thus counts as a local variable of that function. Local variables are not available from outside the function body.

Máté Safranka
  • 4,081
  • 1
  • 10
  • 22
  • 2
    An additional note. You *can* execute the method from the console, provided you set a break point inside the scope where the method is available. Once the breakpoint is hit, and while it is paused, you can run the method from the console. – Taplar Mar 27 '18 at 15:23