OK we'll come back.
It's time for another exercise.
This exercise is going to reinforce NPM and installing packages requiring them running files with Noad
and also some of the basic command line commands.
But in addition to all of that it's also going to force you to be a little bit resourceful and to read
the docs to find out how a particular package works.
So here's a rundown of what you need to do.
Create a directory named my shop at a file named list products.
J.S. inside of the my shop directory and then you need to install the faker package fakers a package
that is actually pretty widely used.
It helps you really really easily and quickly create random fake data.
So if you're testing out your application and you don't have users but you want to fill it out with
fake user data that's not all identical.
So you want to generate a 100 different phone numbers that follow a particular format or 100 fake addresses
that all have a state and zip code in the city or you want to generate random names.
It will even do things like generate random colors and random startup slogans random hacker jargon.
It definitely has a little bit of a sense of humor in some of the things that it generates.
But what's important is that it's a package that you don't know yet and you need to go install it and
then you need to figure out how it works and reassure you right now that it is simple.
It's just one or two lines that you'll need from the faker docs to make this work.
So we're not talking about some crazy exercise that is you know push you really really hard but I am
asking you to approach this as if it was real life and you were trying to figure out how to generate
fake data and the data that I want you to generate is 10 random product names and 10 random prices and
failure has both of those as options.
So you need to generate two different things and print them out and I'll show you what it looks like
.
Node list products such as.
And this is the output I'm looking for yours will look quite a bit different because of the product
names like small cots and computer Tastee granite computer fantastic rubber mouse.
Those are all randomized.
And the prices are also randomized.
All that I'm looking for is 10 lines where we have a random product a dash and a random price.
And if you'd like go ahead and format it nicely like this.
We have a little welcome message but that's not really the focus of this whole thing.
It's much more about NPM and installing things and then also reading the docs and figuring out how it
works.
So I will give you the hint that it is just called faker on NPM.
If you just search for faker the first thing that comes up and you want to take a look at some of these
examples and that's all that I'll say.
So have fun with this.
This is one of those exercises that I really strongly recommend that you do.
I recommend that you do all of them but this one in particular is going to activate a bunch of different
important parts of your developer brain.
So I highly highly recommend that you do it.
Don't just cheat and fast forward to see how I do it.
Take the time and go through it on your own.
And then as always when you are ready once you try it out and you either get it to work or you get stuck
then post a video and I'll be back with a solution.
All right see you in a little bit
.
So whether you got to work 100 percent or if you get it to work partially or if you're just ignoring
everything that I said and just fast forwarding I'm going to go over the solution right now.
I'll start by clearing the consul here and the first thing we need to do is make a directory named my
shop so make directory my shop and then we'll go inside of that directory CD my shop.
Great make a file called list products.
Yes.
So touch list products that genius.
And then we need to install the faker package.
So we need to type NPM install F.A..
We'll take a moment and then as soon as it finishes we'll see that we have a new node modules directory
.
You can see that here you could also see it here for tape LS node modules and inside of it we have faker
and all for F.A. logic and all the code that goes along with it.
So close out of it and let's open up our file here.
List products that Julius the very first thing that we want to do is import faker.
So we're going to have a require faker and we'll set it equal to a variable.
I'll just call it faker.
Far F.A. ECOs acquire F.A. and that's it.
And if we want to make sure that it works we can just run node list products that Jey us and as long
as you don't get an error then we're all good.
If I did have something in here like far F.A. equals require faker with two hours and then I tried to
run the file.
You can see it gives me an error.
Can not find a module faker was to ours.
So it found the module which is great and it clear that error away.
Go back to faker with one r.
Now let's open up the faker docs and take a look at their example that they provide that do you have
an example of how to use faker in a browser.
That's not what we're doing.
We're looking for the no G-S example you can see they required it just like we did and then they're
doing three different things.
Let's just copy these over and see how they work.
So this is always a good place to start.
If a package ever has any sort of usage introduction or getting started code snippet I always go for
that first.
So let's go back and just paste that in.
It looks like this code will generate a random name or a random email and a random contact card.
So let's run this and we'll just do one at a time.
Let's comment out these here and we'll just do random name and we'll constantly log random name at the
end console the log random name just like that and save and we'll run our file node list products up
yes and we get a new random name Eugene Parker MS. And we'll do one more two more.
There we go.
Random names.
Let's try the same thing now but with random e-mail so it will just change what we're printing out.
Just the variable name.
And we'll run it again.
Now we get these new random e-mails.
And lastly we'll do it with the random card even though we don't need to use any of these for the exercise
that I ask you to do.
It's good just to get familiar with how it works.
So everything is faker dot something.
That's something else.
So F.A. dot some sort of category like internet or name and then a particular method like find a name
or e-mail.
Now let's try a random card and just see what that gives us.
Wow.
OK so a lot of information here.
Tons and tons of information including e-mail name user name and address phone number Web site.
A company that they work for that it has a catchphrase like Frontline real time success and some B.S.
as well.
Visionary matrix metrics.
Also some posts I guess like blog posts or comments all sorts of fun information and useful information
.
If we wanted to cede our database we wanted to have some starter data without having to sign up real
users.
This is where failure becomes really handy.
But of course this is not what we're trying to do.
So I'm actually going to delete all of this.
What we're trying to do is print out a random product with a random price and then do that 10 times
.
Let's go back to the faker docs and scroll down a bit and towards the bottom we see this section called
the API methods and this shows all of the different methods all the different things that faker can
generate for us.
And the category that we access them under.
So we have to address all different address components.
And next is commerce which sounds like you could have some stuff that we're looking for and it does
.
So product name definitely we could do commerce product name.
And what do you know.
Right below product name.
We also have price.
So the two things that we're looking for under commers our product name and price.
Let's test them out and we'll just do one first.
So faker dot commers dot product name just like that.
It's a method and we'll print it out.
So cancel that log just like that and I'll clear my console again and run the file.
And we got awesome metal bike and Walmer time sleek fresh clothes and rustic concrete Toona.
That's a great one.
So that will generate us a product name.
Now let's try it for a price.
So I'll just duplicate this line which in cloud nine and in sublime you can do with command shift D
.
And I'll comment the first one out.
And just to double check it was commerce price.
So we're going to do faker dark commerce not price and it's also a method.
All of them are.
And I'll save that and just hit the up arrow hit enter and now we're getting a random price.
Great.
Next thing we want to do is combine them into a single line and then we'll worry about repeating it
10 times.
So we want to combine it so that it looks like product name dash and then the price.
So if we can just do a single line where we constantly log one time just like this.
So we'll consulate log the product name plus and we can just start with that product name.
Plus the price and save.
And if we hit the up arrow we get Tastee caught in car 335 point seven.
Of course we want a space in a dash.
So we need to go back to our string concatenation and add in a new string with a space and with the
dash and another space and then the other plus sign.
And now we add those together and hit the up arrow and run it again.
We now get refined concrete pizza dash 38 points year 9.
Wow that's really a great price for a concrete pizza especially the refined concrete pizza.
All right our rate one minor thing that we are missing is a dollar sign.
So in my example I did have a dollar sign and it looked like refined concrete pizza Dasch dollar sign
thirty eight point zero nine.
So we can just add that right here just like that or if we really wanted to which is not really a good
idea but we could add it separately.
That's a dollar sign but it makes a lot more sense just to use the string that we already have.
So dash space dollar sign and now will save and I'll clear this out and run it again.
Now we get intelligence.
Would any computer at the bargain price of 88 cents with the dollar sign.
So the last thing we need to do is repeat this 10 times by now you should be an expert at repeating
things.
We can use a while loop or we can use a for loop.
And as you know by now I definitely prefer for loops they're much shorter most of the time.
So will do a for loop to repeat something 10 times.
And there's a lot of ways of doing that.
I'm just going to start at zero and go well I is less than 10 and add one to each time.
But of course we could go the other direction start at 10 and subtract 1.
We could go from 20 to 30.
There are so many options here but the standard one.
If there was one standard way of doing this is to go from zero up until the number and then all we need
to do is copy our code in here just like that hit save and let's try running it now.
There we go.
We get 10 different products and 10 different prices.
The last really minor thing is to go ahead and add in our little message that says Welcome to my shop
and we can also add in some of those equal signs just spaced it out a bit just like this.
Of course this is totally superfluous.
Not something that I really want you to spend much time on in this case but just to follow through and
to make it look exactly like the example looks like I'll go ahead and add those in and if we run it
now clear give you the full experience and we hit run or we hit enter.
I get woken to my shop and my 10 products with the 10 prices.
OK so that's all we needed to do.
I hope you enjoyed that a little bit.
As I mentioned earlier faker is not only a simple and easy to use package but it's also one that is
used pretty widely.
So it's not just like the knock knock jokes or the cat me packages which are totally useless.
It is something that has real use and that you actually see in the real world and in the next video
going to introduce a new package a really really important one to this course.
The most important package by far which is called Express and we're going to start creating our own
simple web surfers