Skip to content

Commit 69f539d

Browse files
committed
Arrays docs/files added.
1 parent b71b9cd commit 69f539d

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

docs/12-arrays.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 11.27 Arrays
2+
Consider a scenario where you want to store the names of 100 employees within an IT department. This can be done by creating 100 variables and storing the names. However, keeping track of 100 variables is a tedious task and it results in inefficient memory utilization. The solution to this problem is to create an array variable to store the names of 100 employees.
3+
![[Figure 11.33-Memory Utilization Using an Array.png]]
4+
An array is a collection of values stored in adjacent memory locations. These array values are referenced using a common array name. The values of an array variable must be of the same data type. These values that are also referred to as elements and can be accessed by using subscript or index numbers. The subscript number determines the position of an element in an array list.
5+
6+
JavaScript supports two types of arrays that are as follows:
7+
- Single-dimensional array
8+
- Multi-dimensional array
9+
#### What is Adjacent Memory Location (AML)
10+
Think of computer memory like a **row of lockers**:
11+
- Each locker stores a small piece of data.
12+
- Lockers are numbered in order: 1, 2, 3, 4, …
13+
If a program (process) uses lockers **3, 4, 5, and 6**, those lockers are **adjacent** because they are **side by side**.
14+
![[image212132s.png]]
15+
## 11.27.1 Single-Dimensional Array
16+
In a single-dimensional array, the elements are stored in a single row in the allocated memory.
17+
![[Figure 11.35-Single-Dimensional Array.png]]
18+
As shown in figure 11.35, the first element has the index number zero and the last
19+
array element has an index number one less than the total number of element. This arrangement helps in efficient storage of data, In addition, it also helps to sort data easily and track the data length. The array variable can be created using the `Array` object and `new` keyword along with the size of the array element.
20+
21+
The syntax to declare and initialize a single-dimensional array is as follows:
22+
**Syntax**
23+
```
24+
var variable_name = new Array(size); //Declaration
25+
variable_name[index] = 'value';
26+
```
27+
**where**,
28+
`variable_name`: Is the name of the variable.
29+
`size`: Is the number of elements to be declared in the array.
30+
`index`: Is the index position.
31+
32+
**Code Snippet 36:**
33+
Code Snippet 36 shows different ways to declare and initialize a single-dimensional array.
34+
```
35+
<script>
36+
// Declaration using Array Pbject and then Initialization
37+
varmartial_status = newArray(3);
38+
martial_status[0] = 'Single';
39+
martial_status[1] = 'Married';
40+
martial_status[2] = 'Divorced';
41+
42+
// Declaration and Initialization
43+
varmartial_status = newArray('Single', 'Married', 'Divorced');
44+
45+
// Declaration and Initialization Without Array
46+
varmartial_status['Single', 'Married', 'Divorced'];
47+
</script>
48+
```
49+
## 11.28 Array Methods
50+
An array is a set of values grouped together and identified by a single name. In JavaScript, the `Array` object allows you to create arrays. It provides the `length` property that allows you to determine the number of elements in the array. Various methods of the `Array` object allow to access and manipulate the array elements.
51+
52+
| Method | Description |
53+
| -------- | ---------------------------------------------------- |
54+
| `concat` | Combines one or more array variables. |
55+
| `join` | Joins all the array elements of an array. |
56+
| `pop` | Retrieves the last element of an array. |
57+
| `push` | Appends one or more elements to the end of an array. |
58+
| `sort` | Sorts the array elements in an alphabetical order. |
59+

docs/_sidebar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
* [If-Else](09-if-else.md)
1111
* [Switch Case](10-switch-case.md)
1212
* [String Methods](11-string-methods.md)
13+
* [String Methods](12-arrays.md)
1314
* [Operators Reference](operators.md)

html/12-arrays.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Arrays</title>
7+
</head>
8+
<body>
9+
<script src="../javascript/12-arrays.js"></script>
10+
</body>
11+
</html>

javascript/12-arrays.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
arrayList = ["Steve", "David", "John", "Staffen"];
2+
// console.log(typeof(arrayList));
3+
4+
var demo = new Array(20); // Declaration
5+
// demo = ["Staffen", "David", "John", "Salman", "Arham"];
6+
// demo[0] = "Staffen";
7+
// demo[1] = "John";
8+
// demo[2] = "David";
9+
// demo[3] = "Salman";
10+
// demo[4] = "Arham";
11+
12+
// console.log(arrayList);
13+
14+
/*=================
15+
Array Methods
16+
====================*/
17+
18+
// -----concat()-----
19+
20+
// let arr1 = ["Uzair", "Aftab", "Anas", "Salman"];
21+
// let arr2 = ["Mehwish", "Arham", "Majid", "Zainab"];
22+
23+
// let finalArry = arr1.concat(arr2);
24+
25+
// console.log(finalArry);
26+
27+
// -----join()-----
28+
29+
arr = ["Water", "Fire", "Wind"];
30+
// console.log(arr.join());
31+
32+
arr1 = ["Javascript is fun"];
33+
// console.log(arr1.join(" "));
34+
35+
arr2 = ["1", "2", "3"];
36+
// console.log(arr2.join("-"));
37+
38+
arr3 = ["a", "b", "c"];
39+
// console.log(arr3.join(""));
40+
41+
// -----sort()-----
42+
43+
sampleArr = [5, 2, 3, 4, 6, 1];
44+
console.log(sampleArr.sort()); // Ordererd in Alphabetic

0 commit comments

Comments
 (0)