...

View Full Version : Resolved Function not accessing global array



Steve Comstock
11-18-2010, 10:53 PM
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?

Old Pedant
11-18-2010, 11:00 PM
Your alert is the culprit. You are missing a ' at the very beginning of it:


alert('course_code '+i+' = '+course_code[i]);}

Steve Comstock
11-18-2010, 11:09 PM
Your alert is the culprit. You are missing a ' at the very beginning of it:


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.

Old Pedant
11-18-2010, 11:15 PM
Worked perfectly for me. Here is the *EXACT* code I used:


<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>

Old Pedant
11-18-2010, 11:18 PM
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???

Steve Comstock
11-18-2010, 11:32 PM
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.

Steve Comstock
11-18-2010, 11:35 PM
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]

Old Pedant
11-19-2010, 12:03 AM
You will NEVER believe what the culprit is:


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

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.

Steve Comstock
11-19-2010, 04:04 AM
Well, I'm a believer. I'll look into getting that tomorrow.

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

Steve Comstock
11-19-2010, 03:06 PM
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?

Philip M
11-19-2010, 03:53 PM
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.

Steve Comstock
11-19-2010, 04:21 PM
Done. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum