NSB Cosmic Center Image

N.S.B. Cosmic Center

NSB Cosmic Center Image

JavaScript & MathScript Interactive Document Generator-Demo-04

This document is generated by N.S.B. JavaScript & MathScript Interactive Document Generator version 001.

Introduction to N.S.B. JavaScript & MathScript Interactive Document Generator

JavaScript Arrays

In JavaScript, an array is a very useful object. It can be used to make a list of data and objects, whether of the same or of different types. For instance, this is an array of numbers and strings:

input(1)=> aa=[ 2.3, 'Beauty', 11, 'Eternity', pi, 37, deg, sqrt2 ]
output(1)=> [ 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414 ]

Notice that the identifiers pi, deg, and sqrt2, like many other constants are defined by the program; their values are displayed in the output.

Now, recall that the length of the array is obtained by the standard JavaScript property:

input(2)=> aa.length
output(2)=> 8

Other array-associated methods that are basic in JavaScript are push() and unshift(); these modify the array by adding elements from the right and the left respectively.

input(3)=> aa.push('Mirror'); aa.unshift(speedOfLight)
output(3)=> 10

Notice that the return value above is the new length of the array. The modified array takes the form:

input(4)=> aa
output(4)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

The methods push() and unshift(), apart from their ugly names, have the effect of modifying the existing array. However, in this program, we define two functions appendArr() and prependArr(); these generate new arrays from an existing array. First consider this:

input(5)=> $(1)
output(5)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

Notice that even recalling output(1) gives a modified result. This is due to the fact that the stored object is just a reference to the array which gets modified. Now let us apply appendArr() and prependArr():

input(6)=> bb=appendArr(aa, constGravity)
output(6)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror, 6.673e-11 ]
input(7)=> cc=prependArr(aa,constHbar) 
output(7)=> [ 1.055e-34, 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

Here, we have defined two new arrays bb and cc. Notice that, like the speedOfLight, the program defines the two physical constants constGravity and constHbar (Planck's constant divided by 2π, usually denoted by ħ).

The function copyArr() gives a new array by copying another:

input(8)=> copyArr(aa)
output(8)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

The function joinArr() gives a new array from the elements of two other arrays:

input(9)=> joinArr(bb,cc)
output(9)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror, 6.673e-11, 1.055e-34, 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

The function reverseArr() gives a new array by reversing the order of elements of a given array; so let us apply this function to the above result:

input(10)=> reverseArr($())
output(10)=> [ Mirror, 1.414, 0.01745, 37, 3.142, Eternity, 11, Beauty, 2.300, 299792458, 1.055e-34, 6.673e-11, Mirror, 1.414, 0.01745, 37, 3.142, Eternity, 11, Beauty, 2.300, 299792458 ]

The function holdCasesArr() gives a new array by deleting all elements that do not satisfy a certain condition. For instance, the following keeps only the elements that are strings:

input(11)=> holdCasesArr(bb, isStr)
output(11)=> [ Beauty, Eternity, Mirror ]

The following keeps only the elements that are numbers:

input(12)=> holdCasesArr(cc,isNum)
output(12)=> [ 1.055e-34, 299792458, 2.300, 11, 3.142, 37, 0.01745, 1.414 ]

From the above, this retains only the elements that are integers:

input(13)=> holdCasesArr($(), isInt)
output(13)=> [ 299792458, 11, 37 ]

On the other hand, we have the function delCasesArr() that gives a new array by deleting those elements that satisfy a certain condition:

input(14)=> delCasesArr(cc, isStr)
output(14)=> [ 1.055e-34, 299792458, 2.300, 11, 3.142, 37, 0.01745, 1.414 ]
input(15)=>  delCasesArr($(), isInt) 
output(15)=> [ 1.055e-34, 2.300, 3.142, 0.01745, 1.414 ]
input(16)=> delCasesArr($(), isXpn) 
output(16)=> [ 2.300, 3.142, 0.01745, 1.414 ]

If you like to add or multiply the above numbers, just do this:

input(17)=>  add.apply(null,$())
output(17)=> 6.873
input(18)=>  mult.apply(null,$(16))
output(18)=> 0.1783

This program includes many other functions that deal with JavaScript arrays. Other demodocs will give examples of them. We end this document by illustrating uniteArr() and intersectArr().

input(19)=> bb
output(19)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror, 6.673e-11 ]
input(20)=> cc 
output(20)=> [ 1.055e-34, 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]
input(21)=> uniteArr(bb,cc) 
output(21)=> [ 0.01745, 1.055e-34, 1.414, 11, 2.300, 299792458, 3.142, 37, 6.673e-11, Beauty, Eternity, Mirror ]
input(22)=> intersectArr(bb,cc) 
output(22)=> [ 299792458, 2.300, Beauty, 11, Eternity, 3.142, 37, 0.01745, 1.414, Mirror ]

Notice that uniteArr() gives a new array whose elements are the union of elements of two other arrays, and puts them in a sorted order, while intersectArr() gives the elements that are common to both.

Next
NSB Cosmic Center Image

Home
N.S.B. Cosmic Center

NSB Cosmic Center Image