JavaScript Arrays

 

 

Arrays

 

Creating Arrays

// 1. Create an empty array with no elements

//

var obj_array = new Array();

obj_array[0] = 10.2;

obj_array[1] = "HTML"

obj_array[2] = false;

 

// 2. Create an array with explicit elements

var obj_array = [10.2, "HTML", false];

 

// 3. Create an array with a specific length

var obj_array = new Array(3);

 

Other Array Examples:

 

Arrays for Special Events and Day:

 

paymentCalender = new Array;

paymentCalender [1] = "Class Begin Jan. 8";

paymentCalender [2] = "Late Registration and Drop/Add: Jan. 8-12";

paymentCalender [3] = "Weekend Class Begin Jan. 12";

paymentCalender [4] = "Last Day for Full Refund: Jan. 12";

paymentCalender [5] = "Payment Deadline: Jan. 12";

 

acamedicCalender = new Array;

acamedicCalender [1] = "Class Begin Jan. 8";

academicCalender [2] = "Martin Luther King Jr. Holiday: Jan. 15";

academicCalender [3] = "Spring Break Begins: March 5-11";

academicCalender [4] = "Last Day to Withdraw: March 16";

academicCalender [5] = "Last Day of Classes and Final Exams: April 30-May 6";

academicCalender [6] = "Commencement: May 9";

 

 

var primes = [ 2, 3, 5, 7, 11];

 

var matrix = [ [ 1, 2, 3],

[ 4, 5, 6],

[ 7, 8, 9]];

 

var baseAddress = 1024;

var tableAddress = [baseAddress, baseAddress+1, baseAddress+2];

 

 

Reading Array Elements

 

 

Writing Array Elements

 

 

Adding New Elements to an Array

 

 

The Array Object

 

Constructor

new Array ( )

new Array (size)

new Array (element0, element1, .., elementN)

 

Properties

length

It contains the size of the array

 

Methods

concat(value, ..)

Concatenate arrays

 

join(separator)

Making or forming a string

Example:

var n = [4, 5, 6];

n_sring = n.join(); // "4, 5, 6"

 

pop()

Remove and return the last element of an array

 

push(value, )

Append elements to an array

 

reverse()

Reverse the elements of an array

Example:

var n = [4, 5, 6];

n.reverse(); // n = [6, 5, 4];

 

shift()

Shift array elements down

 

slice(start, end)

Return a portion of an array

 

sort(oderfunction)

Sort the elements of an array

 

splice(start, deleteCount, value)

Insert, remove, or replace array elements

 

toString()

Covert an array to a string

 

unshift(value,..)

Insert elements at the beginning of an rray

 

 

 

Days and Months:

// var dayOfWeek = new Array (7)

 

dayOfWeek = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

for(day = 0; day < 7; day++)

{

document.writeln(dayOfWeek[day]);

}

 

// var monthOfYear = new Array (13)

monthOfYear = new Array(" ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

 

 

Color Coded Resistors Examples:

 

ResistorColor = new Array ("black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray" "white");

 

ResistorColorCode = new Array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

 

GoldBand3 = 0.1;

GoldBand4 = 0.05;

SilverBand3 = 0.01;

SilverBand4 = 0.1;

NoBand = 0.2;

BrownBand5 = 1E-2;

RedBand5 = 0.1E-2;

OrangeBand5 = 0.01E-2;

YellowBand5 = 0.001E-2;

 

Electrical Material Problem Solving:

 

MetalName = new Array("Silver", "Copper", "Gold", "Aluminum", "Tungsten", "Nickel", "Iron", "Constantan", "Nichrome", "Calorite");

 

MetalRelativeConductivityInPercent = new Array(105, 100, 70.5, 61, 31.2, 22.1, 14, 3.52, 1.73, 1.44);

 

// R = zho* (length/Area)

// zho = R* (Area/Length) -- Ohm-Cm

 

Metal_ResistivityOf_zho_CMOhm_per_Foot = new Array(9.9, 10.37, 14.7, 17.0, 33.0, 47.0, 74.0, 295.0, 600.0, 720.0)

 

Metal_Resistivity_zho_Ohm_Cm = new Array (1.645E-6, 1.723E-6, 2.443E-6, 2.825E-6, 5.485E-6, 7.811E-6, 12.299E-6, 15.54E-6, 99.72E-6,250E-6);

 

// Inferred absolute temperature -- Resistance = 0.0 at this temperature

MaterialAbsTemp_in_deg_C = new Array (-243, -234.5, -274, -236, -204, -147, -162, -12500, -2250, Null);

 

MaterialTemp_Coeff_Alpha = new Array (0.0038, 0.00393, 0.0034, 0.00391, 0.005, 0.006, 0.0055, 0.000008, 0.00044, Null);

 

MaterialName = new Array("Air", "Porcelain", "Oils", "Bakelite", "Rubber", "Paper", "Teflon", "Glass", "Mica");

 

MaterialBreakkDwonStength_kv_per_cm = new Array(30, 70, 140, 150, 270, 500, 600, 900, 2000);

 

Electrical Wiring Problem Solving:

 

WireTableAWG_no = new Array ("0000", "000", "00", "0", "1", "2", "3,", "4", "6", "8", "10", "12", "14");

 

WireTableArea_CM = new Array (211600, 167810, 133080, 105530, 83694, 66373, 52634, 41742, 26250, 16509, 10381, 6529.0, 4106.9);

 

WiteTableOhm_per_1000ft_at_20C = new Array (0.0490, 0.0618, 0.0780, 0.0983, 0.1240, 0.1563, 0.1970, 0.2485, 0.3951, 0.6282, 0.9989, 1.588, 2.525);

 

WiteTableMaxAmp_RHW_Insulation = new Array (230, 200, 175, 150, 130, 115, 100, 85, 65, 50, 30, 20, 15);

 

 

Home Appliance Applications:

WattRatingHouseAppliance = new Array ("AirConditioner", "DishWasher", "WashingMachine", "ClothDryer", "WaterHeater", "Heater","FurnanceMotor", "OilBurnerMotor', "MicrowaveOven", "Range", "Refrigerator", "Toaster", "Coffemaker");

 

WattRatingHouseApp_Watts = new Array (860, 1200, 500, 4800, 4500, 1400, 320, 230, 1200, 12,200, 1800, 1200, 900);

 

BGroundColors = new Array("red", "white", "blue");

 

ImageArray = new Array ("images/bulbon.gif", "images/bulboff.gif");