Skip to content

Commit c163917

Browse files
committed
feat: start working with arrays section
1 parent cc16db4 commit c163917

File tree

9 files changed

+790
-0
lines changed

9 files changed

+790
-0
lines changed

11_Working_with_Arrays/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"arrowParens": "avoid"
4+
}
834 KB
Loading

11_Working_with_Arrays/README.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Simple Array Methods
2+
3+
Methods are functions attached to objects and since arrays have methods themselves, arrays are basically objects as well.
4+
5+
We use this built-in methods for manipulating arrays and handling several operations.
6+
7+
## Slice Method (Array.slice(start index, end index))
8+
9+
Similar to the slice method on strings, we use this method to extract parts of an array without changing the main array.
10+
11+
We simply do this by using the `.slice()` keyword and pass in the start and end index we want to slice from.
12+
13+
```js
14+
const arr = ['a', 'b', 'c', 'd', 'e'];
15+
arr.slice(2); // Result will be ['c', 'd', 'e']
16+
```
17+
18+
This will slice the first [0], second [1] elements and start extracting at position [2] of the array, as defined in the method.
19+
20+
We can also define start and end positions of this method.
21+
22+
```js
23+
arr.slice(2, 4); // ['c', 'd' ]
24+
```
25+
26+
What this will basically do is return [2] and [3]. And remember the length of the output array will be the end position minus the start position.
27+
28+
So `4 - 2 = 2` and so the result are two elements `['c', d]`
29+
30+
> **Note**
31+
> It does not mutate the original array but rather creates a copy or clone from the original array with the extracted parts
32+
33+
We can also use negative numbers. For example
34+
35+
```js
36+
arr.slice(-2); // This will give us the last two elements of the array.
37+
```
38+
39+
Finally we can use the slice method to create a shallow copy of the array which is similar to how we used the spread operator to create a shallow copy of the array.
40+
41+
```js
42+
arr.slice(); // ['a', 'b', 'c', 'd', 'e']
43+
[...arr]; // ['a', 'b', 'c', 'd', 'e']
44+
```
45+
46+
You can use either methods to create a shallow copy of an array, but you only use the `.slice()` method when we want to chain multiple array methods.
47+
48+
## Splice Method (Array.splice(start index, delete count))
49+
50+
This works the same way with the slice method, except it mutates the original array.
51+
52+
```js
53+
arr.splice(2); // ['c', 'd', 'e']
54+
console.log(arr); // Remaining elements after splice method ['a', 'b']
55+
arr.splice(-1);
56+
console.log(arr); // returns last element only ['e']
57+
```
58+
59+
In real-world scenario, we don't usually have need for the remaning elements in an array, a good use case of the splice method is removing the last element from an array.
60+
61+
```js
62+
arr.splice(-1); // ['a', 'b' 'c', 'd']
63+
console.log(arr);
64+
```
65+
66+
The splice method also takes in a second argument. Except instead of selecting the last element of the array, it is used to specify the number of elements we want to delete starting from the first index. I.e `deleteCount`
67+
68+
```js
69+
arr.splice(1, 2); // ['b', 'c'] Starts from index [1] and select only 2 elements
70+
```
71+
72+
## Reverse Array.reverse(Array)
73+
74+
The reverse array method is used to reverse the order of an array.
75+
76+
```js
77+
arr = ['a', 'b', 'c', 'd', 'e'];
78+
const arr2 = ['j', 'i', 'h', 'g', 'f'];
79+
console.log(arr2.reverse()); // ['f', 'g', 'h', 'i', 'j']
80+
console.log(arr2); // ['f', 'g', 'h', 'i', 'j']
81+
```
82+
83+
The reverse method also mutates the array. Using this method will reverse the original array.
84+
85+
## Concat Array.concat(object)
86+
87+
This method is used to concat arrays together.
88+
89+
```js
90+
const joinedArr = arr.concat(arr2);
91+
console.log(joinedArr); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
92+
```
93+
94+
The concat method of course does not mutate the original array.
95+
96+
## Join (Array.prototype.join(seperator))
97+
98+
The join method as we've seen is used to concatenate all the elements of an array using a specified seperator string
99+
100+
# Looping Arrays forEach()
101+
102+
Previosuly we used the `for of` loop to loop between arrays, the `forEach` method also allows us to loop between array but in a more different way.
103+
104+
### Looping with for of loop
105+
106+
```js
107+
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
108+
109+
for (const transactions of movements) {
110+
transactions < 1
111+
? console.log(`You withdrew ${Math.abs(transactions)}`)
112+
: console.log(`You deposited ${transactions}`);
113+
}
114+
```
115+
116+
### Looping with forEach
117+
118+
```js
119+
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
120+
121+
movements.forEach(function (transactions) {
122+
transactions < 1
123+
? console.log(`You withdrew ${Math.abs(transactions)}`)
124+
: console.log(`You deposited ${transactions}`);
125+
});
126+
```
127+
128+
The callback function executes at every iteration, so:
129+
130+
```js
131+
0: function(200)
132+
1: function(450)
133+
2: function(-400)
134+
// ...
135+
```
136+
137+
The `forEach` method is a higher order function that takes in a callback function that is executed at it iteration of looping through an array.
138+
139+
As the `forEach` method calls the callback function in each iteration, it will pass in the current element of the array as an argument passed into the function.
140+
141+
The `forEach` method also uses arrow functions.
142+
143+
```js
144+
movements.forEach(transactions =>
145+
transactions < 1
146+
? console.log(`You withdrew ${Math.abs(transactions)}`)
147+
: console.log(`You deposited ${transactions}`)
148+
);
149+
```
150+
151+
### Accessing Index in forEach
152+
153+
Previously we looked at how to access indexes of arrays with the `for of` loop:
154+
155+
```js
156+
for (const [i, transactions] of movements.entries) {
157+
}
158+
```
159+
160+
The `forEach` method also does this and even returns the element, the index, and even the entire array we're looping.
161+
162+
```js
163+
movements.forEach(transcations, i, arr) {
164+
transact < 1
165+
? console.log(`Transaction ${i}: You withdrew ${Math.abs(transact)}`, arr)
166+
: console.log(`Transaction ${i}: You deposited ${transact}`, arr);
167+
}
168+
```
169+
170+
The names does not matter but the order has to be `element, index, array`
171+
172+
> **Note**
173+
> The order of values is different in the `for of` loop and `forEach` method.
174+
175+
`for of (index, element)`
176+
`forEach (element, index, array)`
177+
178+
## forEach or for of Loop?
179+
180+
- The forEach method does not have the `continue` and `break` keywords, so use the for of loop if you absolutely want to break or continue a looping operation.

11_Working_with_Arrays/icon.png

3.83 KB
Loading

11_Working_with_Arrays/index.html

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<link rel="shortcut icon" type="image/png" href="/icon.png" />
8+
<link
9+
href="https://fonts.googleapis.com/css?family=Plus+Jakarta+Sans:200,300,regular,500,600,700,800,200italic,300italic,italic,500italic,600italic,700italic,800italic"
10+
rel="stylesheet"
11+
/>
12+
<link rel="stylesheet" href="style.css" />
13+
<title>Bankist</title>
14+
</head>
15+
<body>
16+
<!-- TOP NAVIGATION -->
17+
<nav>
18+
<img src="logo.png" alt="Logo" class="logo" />
19+
<h2 class="welcome">Log in to get started</h2>
20+
<form class="login">
21+
<label for="username">
22+
Enter Username
23+
<input
24+
type="text"
25+
placeholder="Username"
26+
id="username"
27+
required
28+
class="login__input login__input--user"
29+
/>
30+
</label>
31+
<!-- In practice, use type="password" -->
32+
<label for="pin">
33+
Enter Pin
34+
<input
35+
type="text"
36+
placeholder="Pin"
37+
id="pin"
38+
required
39+
maxlength="4"
40+
class="login__input login__input--pin"
41+
/>
42+
</label>
43+
<button class="login__btn">Log in</button>
44+
</form>
45+
</nav>
46+
47+
<main class="app">
48+
<!-- BALANCE -->
49+
<div class="balance">
50+
<div>
51+
<p class="balance__label">Current balance</p>
52+
<p class="balance__date">
53+
As of <span class="date">05/03/2037</span>
54+
</p>
55+
</div>
56+
<p class="balance__value">0000€</p>
57+
</div>
58+
59+
<!-- MOVEMENTS -->
60+
<div class="movements">
61+
<div class="movements__row">
62+
<div class="movements__type movements__type--deposit">2 deposit</div>
63+
<div class="movements__date">3 days ago</div>
64+
<div class="movements__value">4 000€</div>
65+
</div>
66+
<div class="movements__row">
67+
<div class="movements__type movements__type--withdrawal">
68+
1 withdrawal
69+
</div>
70+
<div class="movements__date">24/01/2037</div>
71+
<div class="movements__value">-378€</div>
72+
</div>
73+
</div>
74+
75+
<!-- SUMMARY -->
76+
<div class="summary">
77+
<p class="summary__label">In</p>
78+
<p class="summary__value summary__value--in">0000€</p>
79+
<p class="summary__label">Out</p>
80+
<p class="summary__value summary__value--out">0000€</p>
81+
<p class="summary__label">Interest</p>
82+
<p class="summary__value summary__value--interest">0000€</p>
83+
<button class="btn--sort">&downarrow; SORT</button>
84+
</div>
85+
86+
<!-- OPERATION: TRANSFERS -->
87+
<div class="operation operation--transfer">
88+
<h2>Transfer money</h2>
89+
<form class="form form--transfer">
90+
<input type="text" class="form__input form__input--to" />
91+
<input type="number" class="form__input form__input--amount" />
92+
<button class="form__btn form__btn--transfer">&rarr;</button>
93+
<label class="form__label">Transfer to</label>
94+
<label class="form__label">Amount</label>
95+
</form>
96+
</div>
97+
98+
<!-- OPERATION: LOAN -->
99+
<div class="operation operation--loan">
100+
<h2>Request loan</h2>
101+
<form class="form form--loan">
102+
<input type="number" class="form__input form__input--loan-amount" />
103+
<button class="form__btn form__btn--loan">&rarr;</button>
104+
<label class="form__label form__label--loan">Amount</label>
105+
</form>
106+
</div>
107+
108+
<!-- OPERATION: CLOSE -->
109+
<div class="operation operation--close">
110+
<h2>Close account</h2>
111+
<form class="form form--close">
112+
<input type="text" class="form__input form__input--user" />
113+
<input
114+
type="password"
115+
maxlength="6"
116+
class="form__input form__input--pin"
117+
/>
118+
<button class="form__btn form__btn--close">&rarr;</button>
119+
<label class="form__label">Confirm user</label>
120+
<label class="form__label">Confirm PIN</label>
121+
</form>
122+
</div>
123+
124+
<!-- LOGOUT TIMER -->
125+
<p class="logout-timer">
126+
You will be logged out in <span class="timer">05:00</span>
127+
</p>
128+
</main>
129+
130+
<!-- <footer>
131+
&copy; by Jonas Schmedtmann. Don't claim as your own :)
132+
</footer> -->
133+
134+
<script src="script.js"></script>
135+
</body>
136+
</html>

11_Working_with_Arrays/logo.png

4.16 KB
Loading

0 commit comments

Comments
 (0)