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");
});
8
  • Post the exact error. Commented Mar 27, 2018 at 15:16
  • Sorry was just editing my post to include. 'calcPCs is not defined' Commented Mar 27, 2018 at 15:17
  • 1
    Including the stack trace would help, as the error doesn't seem to be in the code you've provided. You must be trying to use calcPCs somewhere else. Commented Mar 27, 2018 at 15:18
  • Are you calling calcPCs elsewhere? Commented Mar 27, 2018 at 15:18
  • 1
    @zzzzBov may we have one of them? Commented Mar 27, 2018 at 15:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.