Joint Exercise

Modern JavaScript Learn JavaScript Fundamentals
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.77
List Price:  €131.29
You save:  €37.51
£80.84
List Price:  £113.18
You save:  £32.34
CA$137.53
List Price:  CA$192.54
You save:  CA$55.01
A$155.84
List Price:  A$218.18
You save:  A$62.34
S$136.11
List Price:  S$190.56
You save:  S$54.45
HK$783.07
List Price:  HK$1,096.33
You save:  HK$313.26
CHF 91.04
List Price:  CHF 127.46
You save:  CHF 36.42
NOK kr1,101.67
List Price:  NOK kr1,542.39
You save:  NOK kr440.71
DKK kr700.10
List Price:  DKK kr980.18
You save:  DKK kr280.07
NZ$169.82
List Price:  NZ$237.75
You save:  NZ$67.93
د.إ367.23
List Price:  د.إ514.14
You save:  د.إ146.90
৳10,949.65
List Price:  ৳15,329.95
You save:  ৳4,380.30
₹8,336.20
List Price:  ₹11,671.02
You save:  ₹3,334.81
RM478.35
List Price:  RM669.71
You save:  RM191.36
₦129,687.03
List Price:  ₦181,567.03
You save:  ₦51,880
₨27,766.58
List Price:  ₨38,874.33
You save:  ₨11,107.74
฿3,679.68
List Price:  ฿5,151.71
You save:  ฿1,472.02
₺3,248.23
List Price:  ₺4,547.65
You save:  ₺1,299.42
B$520.25
List Price:  B$728.38
You save:  B$208.12
R1,929.28
List Price:  R2,701.08
You save:  R771.79
Лв183.27
List Price:  Лв256.58
You save:  Лв73.31
₩137,435.75
List Price:  ₩192,415.55
You save:  ₩54,979.80
₪376.07
List Price:  ₪526.51
You save:  ₪150.44
₱5,756.72
List Price:  ₱8,059.64
You save:  ₱2,302.91
¥15,460.95
List Price:  ¥21,645.95
You save:  ¥6,185
MX$1,709.87
List Price:  MX$2,393.89
You save:  MX$684.02
QR364.04
List Price:  QR509.67
You save:  QR145.63
P1,385.73
List Price:  P1,940.08
You save:  P554.35
KSh13,119.90
List Price:  KSh18,368.39
You save:  KSh5,248.48
E£4,832.51
List Price:  E£6,765.71
You save:  E£1,933.20
ብር5,668.96
List Price:  ብር7,936.77
You save:  ብር2,267.81
Kz83,691.63
List Price:  Kz117,171.63
You save:  Kz33,480
CLP$95,228.57
List Price:  CLP$133,323.80
You save:  CLP$38,095.23
CN¥723.85
List Price:  CN¥1,013.42
You save:  CN¥289.57
RD$5,936.90
List Price:  RD$8,311.90
You save:  RD$2,375
DA13,457.95
List Price:  DA18,841.67
You save:  DA5,383.72
FJ$227.72
List Price:  FJ$318.82
You save:  FJ$91.09
Q775.98
List Price:  Q1,086.41
You save:  Q310.42
GY$20,874.15
List Price:  GY$29,224.64
You save:  GY$8,350.49
ISK kr14,123.58
List Price:  ISK kr19,773.58
You save:  ISK kr5,650
DH1,010.74
List Price:  DH1,415.07
You save:  DH404.33
L1,790.82
List Price:  L2,507.22
You save:  L716.40
ден5,773.75
List Price:  ден8,083.48
You save:  ден2,309.73
MOP$804.95
List Price:  MOP$1,126.96
You save:  MOP$322.01
N$1,917.07
List Price:  N$2,683.98
You save:  N$766.90
C$3,695.63
List Price:  C$5,174.03
You save:  C$1,478.40
रु13,325.78
List Price:  रु18,656.63
You save:  रु5,330.84
S/372.26
List Price:  S/521.17
You save:  S/148.91
K380
List Price:  K532.02
You save:  K152.01
SAR375.10
List Price:  SAR525.15
You save:  SAR150.05
ZK2,561.66
List Price:  ZK3,586.43
You save:  ZK1,024.76
L466.89
List Price:  L653.66
You save:  L186.77
Kč2,369.71
List Price:  Kč3,317.69
You save:  Kč947.98
Ft36,989.30
List Price:  Ft51,786.50
You save:  Ft14,797.20
SEK kr1,091.25
List Price:  SEK kr1,527.80
You save:  SEK kr436.54
ARS$87,116.28
List Price:  ARS$121,966.28
You save:  ARS$34,850
Bs690.90
List Price:  Bs967.28
You save:  Bs276.38
COP$391,948.05
List Price:  COP$548,742.95
You save:  COP$156,794.90
₡50,105.09
List Price:  ₡70,149.13
You save:  ₡20,044.04
L2,463.18
List Price:  L3,448.55
You save:  L985.37
₲738,132.47
List Price:  ₲1,033,414.98
You save:  ₲295,282.51
$U3,839.04
List Price:  $U5,374.81
You save:  $U1,535.77
zł404.45
List Price:  zł566.25
You save:  zł161.79
Already have an account? Log In

Transcript

Before we start into the exercises for this section, I want to do a pre exercise just to cover some of the topics that we've dealt with in the fundamental section. So let me jump to sublime. This is what we're going to do. In this little exercise, we're first going to create a date object. And then we're going to print out using console dot log the date in the format of day slash month, slash year. So European format for for printing out the date with slashes.

So let's go ahead and see how that's done. First off, we need to create a variable date and we need to use the date constructor to create a new date object. So that will take care of that for Now we have to extract from this data object, the day of the month, the month itself, and the year so that we can then format it in a day slash month slash year format. So how do we go about doing that? Well, a lot of times, you'll run into JavaScript situations and you can't remember the exact command. You know, there's something out there to do it, but you're not sure what it is.

So let's, and that's a situation with the date object, you don't use the date object all the time. And so that can be something that you have to frequently look up whenever you need to use it. So the best way to do that is simply google. So I'm going to type JavaScript date object in my search, and you'll get a bunch of different references that you can take a look at. I usually like to go through go to W three schools. So as you can see there Give some information about how to use the data object talks about some properties of the data object.

And then it talks about the methods of the date object. So we're going to need to use get date, because that returns the day of the month, get full year because that returns the year. And then also get month returns the month and remember a zero base from zero to 11. So those are the three methods we're going to need to use. So jumping back now, to sublime. Now, there's a couple ways to do this.

Once we have our date object. I'm going to do it by creating a separate variable for the day, month and year and then put those variables together. There are other approaches as approach I'm going to take, I'm going to deal with all the variables at the top. So I'm going to put a comma, tab in and let's get the day first and the day is going to be equal to dot get date. That'll give us the the day. Now let's do month and month will be equal to date, dot get month.

So we're using our dot notation. And then finally the year, and that will be equal to date dot get full year. And then we close that with semicolon. All right, so we've got our variables. Now we need to print that out. However, remember, the month is zero based, so we should do something to the month first.

Before we print it out, we need to add one to it. Remember the increment operator. That's what we're going to use month plus plus semi colon that will increment the month by one. Now this is the nice thing about it. JavaScript being dynamically typed and coercion which we talked about in one of our movies. We don't know for sure what, what format, the data is in coming from the get month method of the date object.

We don't know what format that is, we don't know if it's a string or a number. But because the increment operator is only used with numbers, it will coerce it to a number and then increment it. Okay. And then when we display it, it will convert it back to a string. So now that we have the month incremented, let's go and display console dot log day and concatenate that to slash and then concatenate that to month catenate that to another slash, and then finally concatenate that to year. Then we close it with a semicolon.

All right, that should give us what we need. So we're going to set this This, I'm going to go to my HTML file, I just want to copy the path. It'll make it easier for testing. I'm gonna go ahead and paste in the path and press return. Okay, so here's our, our page. Let me open the console.

And there, there it is. Day is the 21st of July 7 2016. So instead of displaying a six, it displayed a seven because we incremented the month. All right, before we leave this exercise, let's look at how. Let's look at how we would use a template string to to output this. So console dot log.

Then in parentheses, then remember we start with the backtick. And then we want to enter the day first and so we put the dollar sign with the curly braces indicate that the day should go there, then a slash. Then dollar sign, curly braces month, slash dollar sign curly braces year, close out with a semicolon. Let's go ahead and save that. And let's check that one out as well. So if we refresh this page, oh, notice we got an error, syntax error unterminated template literal.

And so the template string literal, I did not terminate properly with a backtick. You probably noticed that as I was typing it in, so let's go back here and put in our back tick right there. Save it again. Go to the page refresh. And there we have it printed twice. Alright, so that reviewed a few concepts that we covered in this section.

Now, let's have you Do a couple of exercises that will also help review

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.