Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Function not accessing global array

    I've read through the past threads and did not find anything that quite
    matched my problem, which surprises me because I thought it would be
    fairly common.

    Anyway, I have a script where I define an array and populate the array.
    In the same script is a function that tries to scan the table looking for
    a match with the value in a global primitive.

    In the HTML, a button click invokes the function. But while the function
    can correctly access the global primitive, it simply stops running when
    it encounters the reference to the global array.

    Here's a shortened, simplified snippet of the code:

    [code]

    <script type="text/javascript">

    var len = 2;
    var course_code = new Array();
    course_code[0] = "A010";
    course_code[1] = "A500";

    function runcodes()
    { alert('In runcodes');
    alert('len = '+len);
    alert('course_code.length = '+course_code.length);
    for (i = 0 ; i < course_code.length ; i++)
    {alert(course_code '+i+' = '+course_code[i]);}
    }

    </script>

    <body>
    <button type="button" name="runc" id="runc" onclick="runcodes()"; >
    Click to display course codes table.
    </button>

    </body>
    [ICODE]


    When I bring this up in a browser and click the button, I get the
    following alerts:

    In runcodes
    len = 2

    and then the script simply stops. So it stops on the first reference
    to the array. Clearly I am getting to function, and the function is
    able to handle the global primitive 'len', but it stops on encountering
    the global array.

    I've tried a lot of variations on the the theme, but nothing gets past
    this restriction. How do I access elements in a global array from inside
    a function?
    Last edited by Steve Comstock; 11-19-2010 at 04:21 PM. Reason: Mark resolved

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Your alert is the culprit. You are missing a ' at the very beginning of it:
    Code:
    alert('course_code '+i+' = '+course_code[i]);}
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Your alert is the culprit. You are missing a ' at the very beginning of it:
    Code:
    alert('course_code '+i+' = '+course_code[i]);}
    Well, good eyes on your part, but I just mistyped that; there really is
    a leading quote; here's a cut and paste:

    alert('course code '+i+' = '+course_code[i]);


    so the problem still persists. In fact, remember, the script execution
    doesn't even get to that statement before it stops.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Worked perfectly for me. Here is the *EXACT* code I used:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    var len = 2;
    var course_code = new Array();
    course_code[0] = "A010";
    course_code[1] = "A500";
    
    function runcodes()
    { 
        alert('In runcodes');
        alert('len = '+len);
        alert('course_code.length = '+course_code.length);
        for (i = 0 ; i < course_code.length ; i++)
        {
           alert('course_code '+i+' = '+course_code[i]);
        }
    }
    
    </script>
    </head>
    <body>
    <form>
    <button type="button" name="runc" id="runc" onclick="runcodes()" >
    Click to display course codes table.
    </button>
    </form>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Quote Originally Posted by Steve Comstock View Post
    In fact, remember, the script execution
    doesn't even get to that statement before it stops.
    ??? How do you know that? An error on that line would terminate execution right there.

    Though, actually, you are right: A syntax error like that SHOULD mean that the code doesn't even *TRY* to run. So NONE of your alerts should show up.

    Maybe you should copy/paste your *REAL* full code???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Excellent! I cut and pasted your code and it worked, too.

    Now to figure out why my version still doesn't work.

    I see you put your <button> control inside a <form>; I tried that on my
    code: still no go.

    Hmmm.

    Oh, oh. I see you put braces around the alert inside the for block...

    Nope. Same thing.


    Let's see; you put double quotes instead of single quotes around the
    array values. I'll try that, but not hopeful.

    Nope. Same result.

    Well, I need to go; still can't see it. But thanks for your insights:
    at least I know it _can_ be done.

    Maybe it's these lines at the top of my code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    I'll look in to that tomorrow.

    Thanks again.

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Fair enough; Here's the whole thing as it stands so far:

    [CODE]

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <!-- Copyright (C) 2006 by Steven H. Comstock Ver1 -->

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    <head>
    <link href="style.css" title="compact" rel="stylesheet" type="text/css" />

    <script type="text/javascript">

    var num = 0;
    var len = 12; /* used to count length of arrays, since '.length' doesn't seem to work! */
    var cname = A010;

    var student_day; /* student day rate based on location */
    var TC = 0; /* Training Cost */
    var baseValue = 100000; /* Initial value */
    var PR = 1.04; /* Productivity Rate Increase */
    var RV; /* Returned Value */

    var cdays = 1; /* 1-5 */
    var ctype = 0; /* beginner, intermediate, or advanced */
    var cloc = 1; /* Denver, continental US outside Denver area, Int'l */
    var csize = 1; /* class size: 1-16 */

    var i = 0;
    var drate = new Array(); /* daily rate based on location */
    drate[0] = 1800; /* local */
    drate[1] = 2200; /* domestic */
    drate[2] = 2600; /* international */

    var course_code = new Array();
    course_code[0] = "A010";
    course_code[1] = "A500";
    course_code[2] = "A633";
    course_code[3] = "A634";
    course_code[4] = "A635";
    course_code[5] = "A650";
    course_code[6] = "A700";
    course_code[7] = "A750";
    course_code[8] = "A780";
    course_code[9] = "A810";
    course_code[10] = "B610";
    course_code[11] = "B620";


    var course_days = new Array();
    course_days[0] = 2; /* A010 duration */
    course_days[1] = 1; /* A500 duration */
    course_days[2] = 3; /* A633 duration */
    course_days[3] = 2; /* A634 duration */
    course_days[4] = 1; /* A635 duration */
    course_days[5] = 3; /* A650 duration */
    course_days[6] = 5; /* A700 duration */
    course_days[7] = 5; /* A750 duration */
    course_days[8] = 2; /* A780 duration */
    course_days[9] = 5; /* A810 duration */
    course_days[10] = 3; /* B610 duration */
    course_days[11] = 3; /* B620 duration */



    function lookupdays()
    {
    alert('lookup');
    alert('cname is '+cname);
    alert('tablesize is '+len);
    alert('sample data is '+course_code[0]);
    for (i = 0 ; i < len ; i++)
    {
    alert('In Loop. i = '+i);
    alert('course_code is '+course_code[i]);
    if (course_code[i]==cname)
    {
    course_days = course_days[i];
    i = len;
    }
    else
    {
    alert('In else');
    }
    }
    }

    function runcodes()
    {
    alert('In runcodes');
    alert('course_code.length = '+len);
    alert('course_code.length = '+course_code.length);
    alert('course code[0] = '+course_code[0]);
    for (i = 0 ; i < course_code.length ; i++)
    {
    alert('course code '+i+' = '+course_code[i]);
    }
    }


    function docalc()
    {baseValue = baseValue * csize;
    TC = course_days * student_day;
    RV = baseValue * PR;
    ROI = RV - baseValue - TC;
    alert('Cost = '+TC+', ROI = '+ROI);
    }

    </script>

    <title>Return On Investment Calculations</title>
    </head>
    <body>
    <p><img src="TTFlogo2.gif" alt="TTFlogo2.gif (2585 bytes)" "width="322" width="322" height="51"></p>

    <h2>Return on Investment Estimator for Classroom Training</h2>
    <p>&nbsp;</p>

    <p>Choose the course being considered .........................

    <select name="course_title" class="select1"
    onchange="num=this.selectedIndex;
    cname=this.options[num].getAttribute('name');
    lookupdays();"
    >
    <option name="A010">Introduction to Application Programming (z/OS)</option>
    <option name="A500">Introduction to z/OS</option>
    <option name="A633">TSO/ISPF in z/OS</option>
    <option name="A634">ISPF Update</option>
    <option name="A635">Advanced ISPF in z/OS</option>
    <option name="A650">TSO CLIST programming in z/OS</option>
    <option name="A700">ISPF and JCL in z/OS</option>
    <option name="A750">TSO REXX programming in z/OS</option>
    <option name="A780">Introduction to TSOand REXX APIs</option>
    <option name="A810">Developing Dialog Applications in z/OS</option>
    <option name="B610">z/OS JCL and Utilities</option>
    <option name="B620">Advanced Topics in z/OS JCL</option>
    </select>
    </p>


    <p>How many students are to be trained (calculations are done for one offering) .....................................
    <select name="course_size"
    onchange="num=this.selectedIndex;
    csize=num+1;"
    >
    <option name="one" > 1</option>
    <option name="two" > 2</option>
    <option name="three" > 3</option>
    <option name="four" > 4</option>
    <option name="five" > 5</option>
    <option name="six" > 6</option>
    <option name="seven" > 7</option>
    <option name="eight" > 8</option>
    <option name="nine" > 9</option>
    <option name="ten" >10</option>
    <option name="eleven" >11</option>
    <option name="twelve" >12</option>
    <option name="thirteen">13</option>
    <option name="fourteen">14</option>
    <option name="fifteen" >15</option>
    <option name="sixteen" >16</option>
    </select>
    </p>


    <p>Where is the training to take place .................................................
    <select name="course_loc" >
    onchange="cloc=this.selectedIndex;
    student_day=drate[cloc];"
    >
    <option name="local">Denver area</option>
    <option name="domestic">Continental US outside Denver area</option>
    <option name="international">Outside continental US</option>
    </select>
    </p>


    <p>Approximate annual average salary for each student (US Dollars) ...............................................
    <input type="text" name="burden" id="burden" size="7" value="100000"
    onchange="baseValue=this.value;" />
    </p>
    <p>&nbsp;</p>

    <button type="button" name="calc" id="calc" onclick="docalc()"; >
    Click to calculate estimated ROI
    </button>

    <button type="button" name="runc" id="runc" onclick="runcodes()"; >
    Click to display course codes table
    </button>

    <p>&nbsp;</p>

    <p name="course_out" id="course_out">
    Estimated out of pocket costs:
    </p>
    <p name="train_out" id="train_out">
    Estimated cost for training time:
    </p>
    <p name="new_value" id="new_value">
    Estimated productivty increase:
    </p>
    <p name="ROI_out" id="ROI_out">
    Estimated ROI for 12 months:
    </p>




    </body>

    </html>



    [ICODE]

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    You will NEVER believe what the culprit is:
    Code:
    var cname = A010;
    If you had run your page in Firefox using FireBug, you would have seen this.

    The error:
    A010 is not defined
    So what happens is that NO FURTHER GLOBAL VARIABLES, past that point in the file, are initialized!!!

    Change the line to
    Code:
    var cname = "A010";
    and everything works.

    *THIS* is why you should get used to using FireBug!!!

    ***********

    p.s.: Please use [/code] instead of [ICODE] at the end of your code blocks in posts.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Steve Comstock (11-19-2010)

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Well, I'm a believer. I'll look into getting that tomorrow.

    Thanks! Works like a champ now. Now I can move forward.

  • #10
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    One last thing. I'm new to this site, and I can't seem to find way to mark this thread as resolved. What do I have to do to accomplish this?

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Steve Comstock View Post
    One last thing. I'm new to this site, and I can't seem to find way to mark this thread as resolved. What do I have to do to accomplish this?
    Follow the instructions shown in Posting Guidelines #6.

  • #12
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Done. Thanks.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •