1
00:00:00,901 --> 00:00:03,668
Here we are in Visual Studio Code in the

2
00:00:03,668 --> 00:00:05,941
JavaScript file of our loan application,

3
00:00:05,941 --> 00:00:09,233
and this is the generateRiskProfile

4
00:00:09,233 --> 00:00:12,095
function. I use this to assess the risk of

5
00:00:12,095 --> 00:00:13,918
lending somebody money based on some

6
00:00:13,918 --> 00:00:16,820
values that the user puts in. The risk is

7
00:00:16,820 --> 00:00:18,941
expressed in a number and later on

8
00:00:18,941 --> 00:00:22,052
translated in a text that says if the risk

9
00:00:22,052 --> 00:00:25,268
is low, medium, or high. You can see that

10
00:00:25,268 --> 00:00:27,624
here, and it changes depending on the

11
00:00:27,624 --> 00:00:30,841
input. Okay, so the first thing I use to

12
00:00:30,841 --> 00:00:33,582
assess the risk is finding out if somebody

13
00:00:33,582 --> 00:00:36,184
is a doctor, PhD, or a medical doctor. Of

14
00:00:36,184 --> 00:00:37,971
course, that's a silly thing to test on,

15
00:00:37,971 --> 00:00:40,575
and I hope that your bank does this

16
00:00:40,575 --> 00:00:42,641
differently, but hey, it works for the

17
00:00:42,641 --> 00:00:45,203
demo. I test the applicant's name, which

18
00:00:45,203 --> 00:00:48,961
also includes the title, like PhD or MD,

19
00:00:48,961 --> 00:00:51,604
and I use the string.search method for

20
00:00:51,604 --> 00:00:54,327
that. This returns the position of the

21
00:00:54,327 --> 00:00:57,436
value within the string and returns -1 if

22
00:00:57,436 --> 00:01:00,228
it doesn't find anything. This results in

23
00:01:00,228 --> 00:01:03,348
a lot of searches and is not the most

24
00:01:03,348 --> 00:01:05,921
readable solution. Let's change this.

25
00:01:05,921 --> 00:01:08,472
Instead of using string.search, I can

26
00:01:08,472 --> 00:01:10,400
determine if the user has entered the

27
00:01:10,400 --> 00:01:12,763
doctor title by using string.startsWith

28
00:01:12,763 --> 00:01:15,591
because doctor is always at the start of

29
00:01:15,591 --> 00:01:18,434
the name, or at least that's what I'm

30
00:01:18,434 --> 00:01:20,761
assuming. This returns a Boolean if it

31
00:01:20,761 --> 00:01:23,750
finds the value dr, and it should also

32
00:01:23,750 --> 00:01:27,271
test for DR in capitals. That is silly.

33
00:01:27,271 --> 00:01:30,442
You know what? Let's do this. I'll trim

34
00:01:30,442 --> 00:01:32,646
the string so that I remove any white

35
00:01:32,646 --> 00:01:34,671
spaces at the beginning and end of the

36
00:01:34,671 --> 00:01:38,301
string, and then I will use toLowerCase to

37
00:01:38,301 --> 00:01:41,270
make the string lowercase. Now I only have

38
00:01:41,270 --> 00:01:43,517
to match the lowercase value. That makes

39
00:01:43,517 --> 00:01:45,581
things a lot more compact. So I'll

40
00:01:45,581 --> 00:01:47,981
implement the rest of the values as well,

41
00:01:47,981 --> 00:01:50,961
including different variations like ph.d

42
00:01:50,961 --> 00:01:53,656
with a period in between. And I assume

43
00:01:53,656 --> 00:01:56,467
that MD is always at the end of a name, so

44
00:01:56,467 --> 00:01:59,941
I'll use string.endsWith to test for that.

45
00:01:59,941 --> 00:02:02,272
And then I need to test for it with a

46
00:02:02,272 --> 00:02:04,796
period in between and also with a period

47
00:02:04,796 --> 00:02:07,377
at the end because endsWith tests for all

48
00:02:07,377 --> 00:02:10,379
the characters at the end of a string just

49
00:02:10,379 --> 00:02:13,513
like startsWith tests for all the starting

50
00:02:13,513 --> 00:02:15,928
characters. All right, now I can remove

51
00:02:15,928 --> 00:02:19,144
all of this and make it a lot simpler. I

52
00:02:19,144 --> 00:02:21,230
just have to test if these are true as

53
00:02:21,230 --> 00:02:23,622
they will be Boolean values, and if they

54
00:02:23,622 --> 00:02:27,740
are, I lower the risk. Nice! And I can

55
00:02:27,740 --> 00:02:29,951
remove the old code and keep the rest.

56
00:02:29,951 --> 00:02:32,769
This looks a lot better, right? Let's see

57
00:02:32,769 --> 00:02:37,821
if it works. Reload the page, and load an

58
00:02:37,821 --> 00:02:40,218
application. Yeah, that seems to still

59
00:02:40,218 --> 00:02:41,961
work. Good. Finally, let me show you this.

60
00:02:41,961 --> 00:02:45,398
StartsWith and endsWith take an optional

61
00:02:45,398 --> 00:02:49,190
parameter of position or length. For

62
00:02:49,190 --> 00:02:50,682
endsWith, this parameter indicates the

63
00:02:50,682 --> 00:02:53,434
length of the string to search. This means

64
00:02:53,434 --> 00:02:55,911
that you can end the search before the end

65
00:02:55,911 --> 00:02:58,205
of the string. For startsWith, this

66
00:02:58,205 --> 00:03:00,681
parameter indicates the position in the

67
00:03:00,681 --> 00:03:03,727
string to start the search at. All right,

68
00:03:03,727 --> 00:03:06,288
let's move on. I also test the string that

69
00:03:06,288 --> 00:03:09,266
contains the purpose for the loan. Here,

70
00:03:09,266 --> 00:03:12,499
people tell us what their purpose is. If I

71
00:03:12,499 --> 00:03:14,627
find any keywords like house or a

72
00:03:14,627 --> 00:03:17,333
vacation, the risk will be influenced. And

73
00:03:17,333 --> 00:03:20,147
again, I use string.search to search for

74
00:03:20,147 --> 00:03:22,715
these keywords. Let's change this. I'll

75
00:03:22,715 --> 00:03:26,268
look for the keyword house by using

76
00:03:26,268 --> 00:03:28,755
string.includes. This returns true if any

77
00:03:28,755 --> 00:03:31,443
of the string contains the value. And just

78
00:03:31,443 --> 00:03:33,765
like earlier, I'll make things easier by

79
00:03:33,765 --> 00:03:37,577
using trim and toLowerCase. Okay, let's

80
00:03:37,577 --> 00:03:40,439
search for the rest of the keywords. I'll

81
00:03:40,439 --> 00:03:44,062
search for holiday, like this, and I'll

82
00:03:44,062 --> 00:03:47,228
also search for vacation here. And

83
00:03:47,228 --> 00:03:49,347
finally, I'll search for business as I

84
00:03:49,347 --> 00:03:51,525
want to know if the loan will be used for

85
00:03:51,525 --> 00:03:54,843
a business. Cool! I can get rid of this

86
00:03:54,843 --> 00:03:57,814
and just test for house. The same goes

87
00:03:57,814 --> 00:04:00,534
here. I can just test for holiday or

88
00:04:00,534 --> 00:04:03,507
vacation. If any of these are true, the

89
00:04:03,507 --> 00:04:06,652
risk increases by three. And finally, I'll

90
00:04:06,652 --> 00:04:10,208
test for business here. And that's it. I

91
00:04:10,208 --> 00:04:12,739
can get rid of the old code. Again, this

92
00:04:12,739 --> 00:04:15,532
looks much cleaner and is much more

93
00:04:15,532 --> 00:04:18,528
readable. Let's test it and reload the

94
00:04:18,528 --> 00:04:21,550
page. And load one. Okay, that still

95
00:04:21,550 --> 00:04:24,552
works. There you go. You can use

96
00:04:24,552 --> 00:04:27,186
string.startsWith and endsWith and

97
00:04:27,186 --> 00:04:29,294
string.includes in addition to functions

98
00:04:29,294 --> 00:04:32,317
like string.search to find values in

99
00:04:32,317 --> 00:04:35,265
strings. And you can make things easier by

100
00:04:35,265 --> 00:04:40,000
using string.trim and toLowerCase or toUpperCase.

