0
1
00:00:00,221 --> 00:00:03,521
Okay so now we're going to work
on the favorite accounts
1

2
00:00:03,521 --> 00:00:07,041
feature so technically it seems
that it's just a regular array
2

3
00:00:07,041 --> 00:00:09,761
of accounts okay we can access
the list of the favorite
3

4
00:00:09,761 --> 00:00:13,281
accounts we can add an account
into the list and we can remove
4

5
00:00:13,281 --> 00:00:16,801
an account to the list by it's ID
that's the only one where we're
5

6
00:00:16,801 --> 00:00:19,521
going to need a little bit more
logic but otherwise it's just a
6

7
00:00:19,521 --> 00:00:24,561
regular accounts array right so
let's go into our project and
7

8
00:00:24,561 --> 00:00:29,681
let's create now a favorite accounts
8

9
00:00:34,721 --> 00:00:41,421
and it's going to be a bank
account array, so we should have a
9

10
00:00:41,421 --> 00:00:43,821
function to be able to add
something into it so I'm going
10

11
00:00:43,821 --> 00:00:52,801
to add account to favorites
and we're going to receive a bank
11

12
00:00:52,801 --> 00:00:55,361
account.
12

13
00:00:57,721 --> 00:01:01,581
And so simply so this is not
going to return anything it's
13

14
00:01:01,581 --> 00:01:06,861
just going to perform an
operation and here we're just
14

15
00:01:06,861 --> 00:01:12,541
going to push this account into
the list so this.favorite
15

16
00:01:12,541 --> 00:01:19,601
accounts.push account and
we're good we should have
16

17
00:01:19,601 --> 00:01:22,321
something to return the
favorite accounts so I'm going
17

18
00:01:22,321 --> 00:01:28,881
to create a get favorites
accounts so this is going to
18

19
00:01:28,881 --> 00:01:34,681
return an array of bank account
so bank account array like this
19

20
00:01:34,681 --> 00:01:38,241
and we don't expect any
parameters because we're going
20

21
00:01:38,241 --> 00:01:41,441
to use only what we have in our
class and we're just going to
21

22
00:01:41,441 --> 00:01:46,721
return this.favorite
accounts so we can try to see
22

23
00:01:46,721 --> 00:01:50,641
if we can add an account and
get the favorite accounts but
23

24
00:01:50,641 --> 00:01:52,841
we're going to have a problem
because we have never
24

25
00:01:52,841 --> 00:01:59,481
initialized the bank accounts
here, okay? Uh we have to set a
25

26
00:01:59,481 --> 00:02:02,761
default value so we should put
anempty array here. Otherwise, you
26

27
00:02:02,761 --> 00:02:06,681
won't be able to push inside
and we'll have an error. So
27

28
00:02:06,681 --> 00:02:10,041
this way it's going to be
initialized. Now we could just
28

29
00:02:10,041 --> 00:02:16,601
test it. So technically I can
just do an account one dot
29

30
00:02:18,721 --> 00:02:22,501
Add account favorites I'm just
going to create a new account
30

31
00:02:22,501 --> 00:02:25,701
so account two
31

32
00:02:26,221 --> 00:02:32,661
I'm going to Create another
one.
32

33
00:02:34,221 --> 00:02:41,221
Like this and I'm going to add
this account to its favorites.
33

34
00:02:41,221 --> 00:02:48,581
And technically I should be
able to see this account if I
34

35
00:02:48,581 --> 00:02:52,741
do a get favorite accounts and
take the first one for example
35

36
00:02:52,741 --> 00:03:01,621
and look at the balance it
should be 100000 so let's see
36

37
00:03:01,621 --> 00:03:08,761
Okay, it's working fine. So
we're good with accessing the
37

38
00:03:08,761 --> 00:03:11,481
favorite accounts, we're good
with adding an account, but now
38

39
00:03:11,481 --> 00:03:14,761
we have to remove an account
from the list by its ID,
39

40
00:03:14,761 --> 00:03:17,961
meaning that an account should
have an ID, which is not the
40

41
00:03:17,961 --> 00:03:25,481
case yet. So we are going to
create an ID that is going to
41

42
00:03:25,481 --> 00:03:30,121
be a number. And we're going to
receive this ID okay so we will
42

43
00:03:30,121 --> 00:03:34,281
assume that it's not generated
but sent through the
43

44
00:03:34,281 --> 00:03:39,001
constructor so I'm going to add
an ID that is going to be a
44

45
00:03:39,001 --> 00:03:43,881
number maybe I can put that on
first parameter like this okay
45

46
00:03:43,881 --> 00:03:51,841
we're going to receive this ID
from the constructor good so
46

47
00:03:51,841 --> 00:03:56,481
now technically here this could
be the ID1 and this could be
47

48
00:03:56,481 --> 00:04:01,521
the ID2 and I need to create a
function to remove an account
48

49
00:04:01,521 --> 00:04:05,361
from the favorite list by its
ID so we're going to create a
49

50
00:04:05,361 --> 00:04:12,481
remove account by ID remove
favorite account by ID and
50

51
00:04:12,481 --> 00:04:16,801
we're going to receive an ID
and this is not going to return
51

52
00:04:16,801 --> 00:04:21,181
anything it's just going to
perform an operation so in the
52

53
00:04:21,181 --> 00:04:24,781
end what we're going to do is
we're going to look at the
53

54
00:04:24,781 --> 00:04:28,381
favorite accounts and just
remove one element so we're
54

55
00:04:28,381 --> 00:04:31,501
going to use the function
splice and and with splice
55

56
00:04:31,501 --> 00:04:34,061
basically you can say that you
want to remove exactly one
56

57
00:04:34,061 --> 00:04:38,541
element and here you're going
to say starting from where from
57

58
00:04:38,541 --> 00:04:41,421
which index so we need the
index to remove this one
58

59
00:04:41,421 --> 00:04:45,821
element so we're going to use
the find by index so we're
59

60
00:04:45,821 --> 00:04:50,681
going to take the favorite
accounts and find by index and
60

61
00:04:50,681 --> 00:04:54,601
here you can send a function so
here we're going to run a
61

62
00:04:54,601 --> 00:04:58,681
function for every element so
this is an account so bank
62

63
00:04:58,681 --> 00:05:01,561
account and here we are going
to return the index of the
63

64
00:05:01,561 --> 00:05:04,121
element that match this very
predicate here that we're going
64

65
00:05:04,121 --> 00:05:07,321
to write so something that
returns true or false and we're
65

66
00:05:07,321 --> 00:05:11,081
going to say that if this
account
66

67
00:05:11,721 --> 00:05:17,421
in my list is equal equal to
the ID that I have sent here
67

68
00:05:17,421 --> 00:05:19,981
then I want to return this
element and it's going to
68

69
00:05:19,981 --> 00:05:29,721
return me the index so I will
have here the index to Remove.
69

70
00:05:29,721 --> 00:05:35,001
And then I can just send this
here but be careful if the
70

71
00:05:35,001 --> 00:05:40,121
element is not found this is
going to return minus one so we
71

72
00:05:40,121 --> 00:05:45,081
have to check that if this is
equal to minus one that's then
72

73
00:05:45,081 --> 00:05:51,161
we're going to throw an error
throw new error account not
73

74
00:05:51,161 --> 00:05:57,321
found in favorites and we
should be good so now we could
74

75
00:05:57,321 --> 00:06:04,221
just try to test it quickly So
we have the ID2 into our list.
75

76
00:06:04,221 --> 00:06:07,421
So I'm going to remove that and
I'm just going to do an account
76

77
00:06:07,421 --> 00:06:12,301
one dot remove favorite account
by ID I'm going to remove the
77

78
00:06:12,301 --> 00:06:19,341
ID2 and then print account one.get
favorite accounts.length
78

79
00:06:19,341 --> 00:06:24,381
technically should be zero
79

80
00:06:25,221 --> 00:06:31,721
Yep, it is, okay, fine. And now
if I try to remove again this,
80

81
00:06:31,721 --> 00:06:38,221
this should trigger an error.
Yep, okay, it's crashing,
81

82
00:06:38,221 --> 00:06:45,061
perfect. Okay, so we're good
with the specification. Now in
82

83
00:06:45,061 --> 00:06:48,501
the next video we're just going
to work on the story. So let's
83

84
00:06:48,501 --> 00:06:51,621
do that in the next one.
