1
00:00:00,120 --> 00:00:01,440
OK we'll come back.

2
00:00:01,590 --> 00:00:03,480
It's time for another exercise.

3
00:00:03,480 --> 00:00:10,170
This exercise is going to reinforce NPM and installing packages requiring them running files with Noad

4
00:00:10,170 --> 00:00:12,430
and also some of the basic command line commands.

5
00:00:12,480 --> 00:00:16,950
But in addition to all of that it's also going to force you to be a little bit resourceful and to read

6
00:00:16,950 --> 00:00:20,580
the docs to find out how a particular package works.

7
00:00:20,580 --> 00:00:22,790
So here's a rundown of what you need to do.

8
00:00:22,860 --> 00:00:27,630
Create a directory named my shop at a file named list products.

9
00:00:27,660 --> 00:00:34,980
J.S. inside of the my shop directory and then you need to install the faker package fakers a package

10
00:00:34,980 --> 00:00:37,330
that is actually pretty widely used.

11
00:00:37,350 --> 00:00:41,890
It helps you really really easily and quickly create random fake data.

12
00:00:42,000 --> 00:00:46,080
So if you're testing out your application and you don't have users but you want to fill it out with

13
00:00:46,080 --> 00:00:48,540
fake user data that's not all identical.

14
00:00:48,540 --> 00:00:54,420
So you want to generate a 100 different phone numbers that follow a particular format or 100 fake addresses

15
00:00:54,420 --> 00:01:00,390
that all have a state and zip code in the city or you want to generate random names.

16
00:01:00,450 --> 00:01:06,890
It will even do things like generate random colors and random startup slogans random hacker jargon.

17
00:01:06,900 --> 00:01:10,750
It definitely has a little bit of a sense of humor in some of the things that it generates.

18
00:01:10,860 --> 00:01:15,990
But what's important is that it's a package that you don't know yet and you need to go install it and

19
00:01:15,990 --> 00:01:20,400
then you need to figure out how it works and reassure you right now that it is simple.

20
00:01:20,400 --> 00:01:24,550
It's just one or two lines that you'll need from the faker docs to make this work.

21
00:01:24,930 --> 00:01:29,400
So we're not talking about some crazy exercise that is you know push you really really hard but I am

22
00:01:29,400 --> 00:01:34,740
asking you to approach this as if it was real life and you were trying to figure out how to generate

23
00:01:34,740 --> 00:01:41,280
fake data and the data that I want you to generate is 10 random product names and 10 random prices and

24
00:01:41,280 --> 00:01:43,340
failure has both of those as options.

25
00:01:43,350 --> 00:01:47,940
So you need to generate two different things and print them out and I'll show you what it looks like

26
00:01:47,940 --> 00:01:48,420
.

27
00:01:48,780 --> 00:01:52,770
Node list products such as.

28
00:01:53,050 --> 00:01:57,900
And this is the output I'm looking for yours will look quite a bit different because of the product

29
00:01:57,900 --> 00:02:04,010
names like small cots and computer Tastee granite computer fantastic rubber mouse.

30
00:02:04,020 --> 00:02:05,290
Those are all randomized.

31
00:02:05,520 --> 00:02:07,320
And the prices are also randomized.

32
00:02:07,450 --> 00:02:15,030
All that I'm looking for is 10 lines where we have a random product a dash and a random price.

33
00:02:15,630 --> 00:02:18,380
And if you'd like go ahead and format it nicely like this.

34
00:02:18,390 --> 00:02:22,180
We have a little welcome message but that's not really the focus of this whole thing.

35
00:02:22,200 --> 00:02:27,990
It's much more about NPM and installing things and then also reading the docs and figuring out how it

36
00:02:27,990 --> 00:02:28,610
works.

37
00:02:28,770 --> 00:02:32,590
So I will give you the hint that it is just called faker on NPM.

38
00:02:32,640 --> 00:02:38,670
If you just search for faker the first thing that comes up and you want to take a look at some of these

39
00:02:38,670 --> 00:02:40,590
examples and that's all that I'll say.

40
00:02:40,740 --> 00:02:42,110
So have fun with this.

41
00:02:42,120 --> 00:02:46,420
This is one of those exercises that I really strongly recommend that you do.

42
00:02:46,560 --> 00:02:51,210
I recommend that you do all of them but this one in particular is going to activate a bunch of different

43
00:02:51,210 --> 00:02:53,310
important parts of your developer brain.

44
00:02:53,430 --> 00:02:55,570
So I highly highly recommend that you do it.

45
00:02:55,650 --> 00:02:58,390
Don't just cheat and fast forward to see how I do it.

46
00:02:58,440 --> 00:03:00,420
Take the time and go through it on your own.

47
00:03:00,750 --> 00:03:06,060
And then as always when you are ready once you try it out and you either get it to work or you get stuck

48
00:03:06,390 --> 00:03:09,390
then post a video and I'll be back with a solution.

49
00:03:09,390 --> 00:03:10,320
All right see you in a little bit

50
00:03:10,320 --> 00:03:16,230
.

51
00:03:16,230 --> 00:03:21,150
So whether you got to work 100 percent or if you get it to work partially or if you're just ignoring

52
00:03:21,150 --> 00:03:25,830
everything that I said and just fast forwarding I'm going to go over the solution right now.

53
00:03:25,830 --> 00:03:30,750
I'll start by clearing the consul here and the first thing we need to do is make a directory named my

54
00:03:30,750 --> 00:03:40,270
shop so make directory my shop and then we'll go inside of that directory CD my shop.

55
00:03:40,560 --> 00:03:44,640
Great make a file called list products.

56
00:03:44,670 --> 00:03:45,310
Yes.

57
00:03:45,540 --> 00:03:50,250
So touch list products that genius.

58
00:03:50,370 --> 00:03:53,520
And then we need to install the faker package.

59
00:03:53,520 --> 00:03:57,960
So we need to type NPM install F.A..

60
00:03:58,620 --> 00:04:04,650
We'll take a moment and then as soon as it finishes we'll see that we have a new node modules directory

61
00:04:04,650 --> 00:04:04,880
.

62
00:04:04,920 --> 00:04:11,760
You can see that here you could also see it here for tape LS node modules and inside of it we have faker

63
00:04:11,970 --> 00:04:15,060
and all for F.A. logic and all the code that goes along with it.

64
00:04:15,360 --> 00:04:18,350
So close out of it and let's open up our file here.

65
00:04:18,360 --> 00:04:24,540
List products that Julius the very first thing that we want to do is import faker.

66
00:04:24,540 --> 00:04:29,430
So we're going to have a require faker and we'll set it equal to a variable.

67
00:04:29,430 --> 00:04:31,170
I'll just call it faker.

68
00:04:31,170 --> 00:04:34,020
Far F.A. ECOs acquire F.A. and that's it.

69
00:04:34,110 --> 00:04:40,770
And if we want to make sure that it works we can just run node list products that Jey us and as long

70
00:04:40,770 --> 00:04:43,350
as you don't get an error then we're all good.

71
00:04:43,440 --> 00:04:50,970
If I did have something in here like far F.A. equals require faker with two hours and then I tried to

72
00:04:51,120 --> 00:04:52,480
run the file.

73
00:04:52,770 --> 00:04:54,090
You can see it gives me an error.

74
00:04:54,120 --> 00:04:57,060
Can not find a module faker was to ours.

75
00:04:57,210 --> 00:05:00,640
So it found the module which is great and it clear that error away.

76
00:05:00,840 --> 00:05:03,240
Go back to faker with one r.

77
00:05:03,240 --> 00:05:09,650
Now let's open up the faker docs and take a look at their example that they provide that do you have

78
00:05:09,640 --> 00:05:12,510
an example of how to use faker in a browser.

79
00:05:12,620 --> 00:05:13,890
That's not what we're doing.

80
00:05:13,910 --> 00:05:19,910
We're looking for the no G-S example you can see they required it just like we did and then they're

81
00:05:19,910 --> 00:05:21,660
doing three different things.

82
00:05:21,980 --> 00:05:24,530
Let's just copy these over and see how they work.

83
00:05:24,520 --> 00:05:26,420
So this is always a good place to start.

84
00:05:26,480 --> 00:05:32,560
If a package ever has any sort of usage introduction or getting started code snippet I always go for

85
00:05:32,560 --> 00:05:33,410
that first.

86
00:05:33,620 --> 00:05:37,250
So let's go back and just paste that in.

87
00:05:37,310 --> 00:05:43,730
It looks like this code will generate a random name or a random email and a random contact card.

88
00:05:44,060 --> 00:05:46,630
So let's run this and we'll just do one at a time.

89
00:05:46,900 --> 00:05:53,870
Let's comment out these here and we'll just do random name and we'll constantly log random name at the

90
00:05:53,870 --> 00:06:03,620
end console the log random name just like that and save and we'll run our file node list products up

91
00:06:03,740 --> 00:06:11,200
yes and we get a new random name Eugene Parker MS. And we'll do one more two more.

92
00:06:11,320 --> 00:06:11,940
There we go.

93
00:06:11,950 --> 00:06:13,090
Random names.

94
00:06:13,390 --> 00:06:19,860
Let's try the same thing now but with random e-mail so it will just change what we're printing out.

95
00:06:20,380 --> 00:06:21,860
Just the variable name.

96
00:06:22,100 --> 00:06:23,400
And we'll run it again.

97
00:06:23,770 --> 00:06:26,310
Now we get these new random e-mails.

98
00:06:26,890 --> 00:06:32,210
And lastly we'll do it with the random card even though we don't need to use any of these for the exercise

99
00:06:32,200 --> 00:06:33,330
that I ask you to do.

100
00:06:33,520 --> 00:06:35,730
It's good just to get familiar with how it works.

101
00:06:35,750 --> 00:06:38,820
So everything is faker dot something.

102
00:06:38,900 --> 00:06:40,070
That's something else.

103
00:06:40,250 --> 00:06:46,820
So F.A. dot some sort of category like internet or name and then a particular method like find a name

104
00:06:47,140 --> 00:06:48,650
or e-mail.

105
00:06:48,640 --> 00:06:53,590
Now let's try a random card and just see what that gives us.

106
00:06:54,370 --> 00:06:54,670
Wow.

107
00:06:54,700 --> 00:06:56,470
OK so a lot of information here.

108
00:06:56,690 --> 00:07:03,120
Tons and tons of information including e-mail name user name and address phone number Web site.

109
00:07:03,250 --> 00:07:09,130
A company that they work for that it has a catchphrase like Frontline real time success and some B.S.

110
00:07:09,130 --> 00:07:09,660
as well.

111
00:07:09,700 --> 00:07:12,040
Visionary matrix metrics.

112
00:07:12,050 --> 00:07:17,680
Also some posts I guess like blog posts or comments all sorts of fun information and useful information

113
00:07:17,690 --> 00:07:17,720
.

114
00:07:17,720 --> 00:07:22,320
If we wanted to cede our database we wanted to have some starter data without having to sign up real

115
00:07:22,340 --> 00:07:23,130
users.

116
00:07:23,360 --> 00:07:26,050
This is where failure becomes really handy.

117
00:07:26,180 --> 00:07:28,240
But of course this is not what we're trying to do.

118
00:07:28,370 --> 00:07:30,290
So I'm actually going to delete all of this.

119
00:07:30,520 --> 00:07:36,520
What we're trying to do is print out a random product with a random price and then do that 10 times

120
00:07:36,550 --> 00:07:36,730
.

121
00:07:36,880 --> 00:07:42,980
Let's go back to the faker docs and scroll down a bit and towards the bottom we see this section called

122
00:07:42,990 --> 00:07:48,020
the API methods and this shows all of the different methods all the different things that faker can

123
00:07:48,010 --> 00:07:49,060
generate for us.

124
00:07:49,250 --> 00:07:51,610
And the category that we access them under.

125
00:07:51,880 --> 00:07:55,910
So we have to address all different address components.

126
00:07:55,910 --> 00:08:00,980
And next is commerce which sounds like you could have some stuff that we're looking for and it does

127
00:08:00,980 --> 00:08:01,400
.

128
00:08:01,390 --> 00:08:05,090
So product name definitely we could do commerce product name.

129
00:08:05,210 --> 00:08:06,030
And what do you know.

130
00:08:06,050 --> 00:08:07,180
Right below product name.

131
00:08:07,220 --> 00:08:08,480
We also have price.

132
00:08:08,530 --> 00:08:13,280
So the two things that we're looking for under commers our product name and price.

133
00:08:13,610 --> 00:08:16,300
Let's test them out and we'll just do one first.

134
00:08:16,370 --> 00:08:23,240
So faker dot commers dot product name just like that.

135
00:08:23,410 --> 00:08:25,740
It's a method and we'll print it out.

136
00:08:25,780 --> 00:08:34,360
So cancel that log just like that and I'll clear my console again and run the file.

137
00:08:34,660 --> 00:08:42,600
And we got awesome metal bike and Walmer time sleek fresh clothes and rustic concrete Toona.

138
00:08:42,640 --> 00:08:43,670
That's a great one.

139
00:08:43,900 --> 00:08:46,240
So that will generate us a product name.

140
00:08:46,250 --> 00:08:47,860
Now let's try it for a price.

141
00:08:47,890 --> 00:08:54,350
So I'll just duplicate this line which in cloud nine and in sublime you can do with command shift D

142
00:08:54,470 --> 00:08:54,810
.

143
00:08:55,020 --> 00:08:56,480
And I'll comment the first one out.

144
00:08:56,890 --> 00:09:00,420
And just to double check it was commerce price.

145
00:09:00,670 --> 00:09:05,470
So we're going to do faker dark commerce not price and it's also a method.

146
00:09:05,570 --> 00:09:06,550
All of them are.

147
00:09:06,760 --> 00:09:15,300
And I'll save that and just hit the up arrow hit enter and now we're getting a random price.

148
00:09:15,350 --> 00:09:16,390
Great.

149
00:09:16,390 --> 00:09:21,290
Next thing we want to do is combine them into a single line and then we'll worry about repeating it

150
00:09:21,280 --> 00:09:22,150
10 times.

151
00:09:22,390 --> 00:09:31,210
So we want to combine it so that it looks like product name dash and then the price.

152
00:09:31,490 --> 00:09:38,030
So if we can just do a single line where we constantly log one time just like this.

153
00:09:38,300 --> 00:09:43,720
So we'll consulate log the product name plus and we can just start with that product name.

154
00:09:43,730 --> 00:09:46,480
Plus the price and save.

155
00:09:46,490 --> 00:09:52,480
And if we hit the up arrow we get Tastee caught in car 335 point seven.

156
00:09:52,580 --> 00:09:54,830
Of course we want a space in a dash.

157
00:09:54,830 --> 00:10:00,920
So we need to go back to our string concatenation and add in a new string with a space and with the

158
00:10:00,910 --> 00:10:04,840
dash and another space and then the other plus sign.

159
00:10:05,600 --> 00:10:10,430
And now we add those together and hit the up arrow and run it again.

160
00:10:10,490 --> 00:10:15,590
We now get refined concrete pizza dash 38 points year 9.

161
00:10:15,800 --> 00:10:20,130
Wow that's really a great price for a concrete pizza especially the refined concrete pizza.

162
00:10:20,360 --> 00:10:23,900
All right our rate one minor thing that we are missing is a dollar sign.

163
00:10:24,160 --> 00:10:30,100
So in my example I did have a dollar sign and it looked like refined concrete pizza Dasch dollar sign

164
00:10:30,130 --> 00:10:31,610
thirty eight point zero nine.

165
00:10:31,750 --> 00:10:37,850
So we can just add that right here just like that or if we really wanted to which is not really a good

166
00:10:37,850 --> 00:10:40,130
idea but we could add it separately.

167
00:10:40,120 --> 00:10:44,930
That's a dollar sign but it makes a lot more sense just to use the string that we already have.

168
00:10:45,110 --> 00:10:53,810
So dash space dollar sign and now will save and I'll clear this out and run it again.

169
00:10:53,870 --> 00:10:54,910
Now we get intelligence.

170
00:10:54,910 --> 00:10:59,240
Would any computer at the bargain price of 88 cents with the dollar sign.

171
00:10:59,480 --> 00:11:04,570
So the last thing we need to do is repeat this 10 times by now you should be an expert at repeating

172
00:11:04,580 --> 00:11:05,100
things.

173
00:11:05,260 --> 00:11:07,730
We can use a while loop or we can use a for loop.

174
00:11:08,090 --> 00:11:12,700
And as you know by now I definitely prefer for loops they're much shorter most of the time.

175
00:11:12,740 --> 00:11:15,720
So will do a for loop to repeat something 10 times.

176
00:11:15,880 --> 00:11:17,470
And there's a lot of ways of doing that.

177
00:11:17,500 --> 00:11:23,910
I'm just going to start at zero and go well I is less than 10 and add one to each time.

178
00:11:24,080 --> 00:11:28,240
But of course we could go the other direction start at 10 and subtract 1.

179
00:11:28,310 --> 00:11:30,640
We could go from 20 to 30.

180
00:11:30,640 --> 00:11:32,690
There are so many options here but the standard one.

181
00:11:32,700 --> 00:11:38,650
If there was one standard way of doing this is to go from zero up until the number and then all we need

182
00:11:38,650 --> 00:11:46,360
to do is copy our code in here just like that hit save and let's try running it now.

183
00:11:47,600 --> 00:11:48,160
There we go.

184
00:11:48,190 --> 00:11:52,100
We get 10 different products and 10 different prices.

185
00:11:52,100 --> 00:11:59,120
The last really minor thing is to go ahead and add in our little message that says Welcome to my shop

186
00:11:59,990 --> 00:12:08,510
and we can also add in some of those equal signs just spaced it out a bit just like this.

187
00:12:08,650 --> 00:12:10,720
Of course this is totally superfluous.

188
00:12:10,820 --> 00:12:16,540
Not something that I really want you to spend much time on in this case but just to follow through and

189
00:12:16,550 --> 00:12:21,950
to make it look exactly like the example looks like I'll go ahead and add those in and if we run it

190
00:12:21,940 --> 00:12:28,310
now clear give you the full experience and we hit run or we hit enter.

191
00:12:28,420 --> 00:12:32,560
I get woken to my shop and my 10 products with the 10 prices.

192
00:12:32,790 --> 00:12:34,520
OK so that's all we needed to do.

193
00:12:34,790 --> 00:12:36,400
I hope you enjoyed that a little bit.

194
00:12:36,400 --> 00:12:42,710
As I mentioned earlier faker is not only a simple and easy to use package but it's also one that is

195
00:12:42,700 --> 00:12:43,970
used pretty widely.

196
00:12:44,200 --> 00:12:50,230
So it's not just like the knock knock jokes or the cat me packages which are totally useless.

197
00:12:50,410 --> 00:12:56,020
It is something that has real use and that you actually see in the real world and in the next video

198
00:12:56,130 --> 00:13:00,160
going to introduce a new package a really really important one to this course.

199
00:13:00,160 --> 00:13:04,840
The most important package by far which is called Express and we're going to start creating our own

200
00:13:04,850 --> 00:13:05,880
simple web surfers
