Arrays
Lesson 20
In this Lesson
What is an Array?
In This Lesson
Using for Statement in Arrays
What is an Array?
What is an Array?
An array contains a set of data
represented by a single variable name.
Formats in Creating an Array
• array_name = newArray(number of elements);
Example: quiz = newArray(4);
Formats in Creating an Array
•array_name = newArray(elements);
Example: grades = newArray(85, 83, 90, 89);
Referring to a specific element:
Example:
quiz[0]
quiz[1]
quiz[2]
quiz[3]
Accessing Array
Assigning values to the elements:
Example:
grades[0] = 90;
grades[l] = 83;
grades[2] = 85;
grades[3] = 92;
Accessing Array
Code: sample21.html
<html>
<head><title> Using Arrays </title></head>
<body>
<script language="JavaScript">
nday = new Array(7);
nday[0] = prompt(“Enter the first day of the
week”,””);
nday[1] = prompt(“Enter the second day of the
week”,””);
nday[2] = prompt(“Enter the third day of the
week”,””);
nday[3] = prompt(“Enter the fourth day of the
week”,””);
nday[4] = prompt(“Enter the fith day of the
week”,””);
nday[5] = prompt(“Enter the sixth day of the
week”,””);
nday[6] = prompt(“Enter the seventh day of the
week”,””);
document.writeln(“The first day of the week is “ +
nday[0]);
document.writeln(“<br>”);
document.writeln(“The last day of the week is “ +
nday[6]);
</script>
</body>
</html>
Output: sample21.html
Using for Statement in Arrays
Code: sample22.html
<html>
<head><title>Using Arrays</title></head>
<body>
<script language="JavaScript">
grade = new Array(4);
sum=0;
for(i=0; i<4; i++){
grade[i] = prompt("Enter the grade","");
sum+=parseInt(grade[i]);
}
average = sum/4;
document.writeln("List of grades: <br>");
for(i=0; i<4; i++){
document.writeln("Quarter " + (i+1) + ": ");
document.writeln(grade[i] + "<br>");
}
document.writeln("Average :" + average);
</script>
</body>
</html>
Output: sample22.html
Answer the activity
on page 102.
Test Your Knowledge
Do the activity on
page 103.
Activity Time
Filename: activity15.html
Filename: activity15a.html

JAVASCRIPT LESSON 20 - LEARNJAVASCRIPT - ARRAYS