Fire-House 58

Arrays

paired

a-2


JavaScript:
firehouse58.com paired arrays are parallel arrays used to store and access paired data.

.

Hard Reload
To See Menu

Team
each player[i] paired to position[i] by row number.

row
#

Array(5)
player
Array(5)
position
[0] Payton Point Guard
[1] Hawkins Shooting Guard
[2] Baker Forward
[3] Schrempf Forward
[4] McIlvaine Center

<head>
<script language="JavaScript">
<!--

paired array

var player = new Array(5)

player[0] = "Gary Payton"
player[1] = "Hersey Hawkins"
player[2] = "Detlef Schrempf"
player[3] = "Vin Baker"
player[4] = "Jim McIlvaine"

This is the first array player. It holds the name of each player, indexed by 5 numbered rows.

var position = new Array(5)

position[0] = "Point Guard"
position[1] = "Shooting Guard"
position[2] = "Forward"
position[3] = "Forward"
position[4] = "Center"

The second array, position holds the position of each player. They are indexed by 5 numbered rows that correspond to the player array.
player[4] = "Jim McIlvaine" corresponds with his position, position[4] = "Center".   

function outpl(form) {
breaker = ""
i = Sonics.playername.selectedIndex

if ( i != 0 ) { i--; }
for ( var j = 0 ; j < i ; j++ ) { breaker += "\n"; continue; }
Sonics.bio.value = breaker + player[i] + " " + position[i]; }

function outpos(form) {
breaker = ""
j = Sonics.posname.selectedIndex
if ( j != 0 ) { j-- }
for ( var i = 0 ; i < j ; i++ ) { breaker += "\n"; continue;}
Sonics.bio.value = breaker + position[j] + " " + player[j] }

function outtm(form) {
build = "The Seattle Sonics 1998"
for ( k = 0 ; k < player.length ; k++ ) {
build += "\n" + player[k] + position[k] }
Sonics.bio.value = build }
//-->
</script>
</head>
<body>

The functions outpl(form), outpos(form), &  outtm(form), build the output. The first two are called by the two select objects 'playername' & 'posname', which  were built with JavaScript using the document.write() method.

The functions outpl(form) & outpos(form) use the value passed to them from the form select objects to determine which player/position pair to display in the textarea bio. They are both offset by one. This is to account for the title positions <option not selected>Starting Line-up & <option not selected>Starting Position which are actually selectedIndex[0] in both select objects.


The function outtm(form) simply cycles through both arrays to build the output to the textarea bio

<form>
<script language="JavaScript">
<!--
var build = ""
build = "<select name='playername'

onChange='outpl(this.form)'>"
build += "<option not selected>Starting Line-up "

for ( var i = 0 ; i < player.length ; i++ ) {
build += "<option>" + player[i] }
build += "</select>"
document.write(build)

This script is actually inserted into the body.


It assigns a variable build to develop output to write to the document. The for loop takes care of populating the select object 'playername' with the items in the player array.

In other words, the array determines what the contents of the select object will be.  

 

buildt = ""
buildt = "<select name='posname'

onChange='outpos(this.form)'>"
buildt += "<option not selected>Starting Position"

for ( var j = 0 ; j < position.length ; j++ ) {
buildt += "<option>" + position[j] }
buildt += "</select>"
document.write(buildt)
//-->
</script>

 

This is essentially a repeat, of building the 'playername' select object. It builds a second select object 'posname' from the position array.

H
I
N
T

Using JavaScript, it is possible to let the elements of an array determine the contents of the select object. Any change in the data of the array would be reflected in the select object. The Array object rows will correspond with the select objects index.


<input type="radio" checked name="R1" onClick="outtm(this.form)">
<textarea rows="6" name="bio" cols="35"></textarea>
</form>
</body>

The rest of the form is actually more of a physical entity. It consists of the radio object "R1", and the textarea bio.  

 

[back]   [home]   [next]