Exercise 2

Modern JavaScript Learn JavaScript Fundamentals
4 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
€92.30
List Price:  €129.23
You save:  €36.92
£77.49
List Price:  £108.49
You save:  £30.99
CA$143.72
List Price:  CA$201.21
You save:  CA$57.49
A$158.70
List Price:  A$222.18
You save:  A$63.48
S$133.07
List Price:  S$186.31
You save:  S$53.23
HK$776.99
List Price:  HK$1,087.82
You save:  HK$310.83
CHF 88.07
List Price:  CHF 123.30
You save:  CHF 35.23
NOK kr1,085.88
List Price:  NOK kr1,520.27
You save:  NOK kr434.39
DKK kr688.37
List Price:  DKK kr963.74
You save:  DKK kr275.37
NZ$175.15
List Price:  NZ$245.23
You save:  NZ$70.07
د.إ367.23
List Price:  د.إ514.14
You save:  د.إ146.90
৳12,163.22
List Price:  ৳17,029
You save:  ৳4,865.77
₹8,714.05
List Price:  ₹12,200.02
You save:  ₹3,485.97
RM441.40
List Price:  RM617.98
You save:  RM176.58
₦151,444.61
List Price:  ₦212,028.51
You save:  ₦60,583.90
₨28,030.77
List Price:  ₨39,244.21
You save:  ₨11,213.43
฿3,375.10
List Price:  ฿4,725.28
You save:  ฿1,350.17
₺3,639.71
List Price:  ₺5,095.74
You save:  ₺1,456.03
B$578.70
List Price:  B$810.20
You save:  B$231.50
R1,824.95
List Price:  R2,555.01
You save:  R730.05
Лв180.24
List Price:  Лв252.35
You save:  Лв72.10
₩144,737.02
List Price:  ₩202,637.62
You save:  ₩57,900.60
₪362.30
List Price:  ₪507.24
You save:  ₪144.93
₱5,719.92
List Price:  ₱8,008.12
You save:  ₱2,288.20
¥14,803.01
List Price:  ¥20,724.81
You save:  ¥5,921.80
MX$2,026.51
List Price:  MX$2,837.20
You save:  MX$810.68
QR364.97
List Price:  QR510.97
You save:  QR146
P1,361.18
List Price:  P1,905.71
You save:  P544.53
KSh12,954.91
List Price:  KSh18,137.39
You save:  KSh5,182.48
E£5,067.36
List Price:  E£7,094.51
You save:  E£2,027.14
ብር13,088.75
List Price:  ብር18,324.78
You save:  ብር5,236.02
Kz91,440.85
List Price:  Kz128,020.85
You save:  Kz36,580
CLP$92,759.60
List Price:  CLP$129,867.15
You save:  CLP$37,107.55
CN¥723.35
List Price:  CN¥1,012.72
You save:  CN¥289.37
RD$6,262.46
List Price:  RD$8,767.70
You save:  RD$2,505.23
DA13,324.71
List Price:  DA18,655.13
You save:  DA5,330.41
FJ$229.65
List Price:  FJ$321.52
You save:  FJ$91.87
Q772.12
List Price:  Q1,081
You save:  Q308.88
GY$20,944.69
List Price:  GY$29,323.41
You save:  GY$8,378.71
ISK kr13,573.13
List Price:  ISK kr19,002.93
You save:  ISK kr5,429.79
DH973.43
List Price:  DH1,362.85
You save:  DH389.41
L1,799.84
List Price:  L2,519.85
You save:  L720.01
ден5,677.46
List Price:  ден7,948.67
You save:  ден2,271.21
MOP$801.29
List Price:  MOP$1,121.84
You save:  MOP$320.55
N$1,814.57
List Price:  N$2,540.47
You save:  N$725.90
C$3,684.40
List Price:  C$5,158.30
You save:  C$1,473.90
रु13,920.16
List Price:  रु19,488.79
You save:  रु5,568.62
S/365.75
List Price:  S/512.07
You save:  S/146.31
K408.76
List Price:  K572.28
You save:  K163.52
SAR375.12
List Price:  SAR525.19
You save:  SAR150.06
ZK2,850.63
List Price:  ZK3,991
You save:  ZK1,140.36
L459.21
List Price:  L642.91
You save:  L183.70
Kč2,301.83
List Price:  Kč3,222.66
You save:  Kč920.82
Ft36,714.80
List Price:  Ft51,402.18
You save:  Ft14,687.38
SEK kr1,008.53
List Price:  SEK kr1,411.99
You save:  SEK kr403.45
ARS$106,479.45
List Price:  ARS$149,075.49
You save:  ARS$42,596.04
Bs691.75
List Price:  Bs968.49
You save:  Bs276.73
COP$410,608.13
List Price:  COP$574,867.80
You save:  COP$164,259.67
₡50,620.11
List Price:  ₡70,870.18
You save:  ₡20,250.07
L2,559.98
List Price:  L3,584.07
You save:  L1,024.09
₲792,694.10
List Price:  ₲1,109,803.45
You save:  ₲317,109.35
$U4,240.13
List Price:  $U5,936.35
You save:  $U1,696.22
zł384.91
List Price:  zł538.90
You save:  zł153.98
Already have an account? Log In

Transcript

We've come to exercise two, let me explain what I'd like you to do in this exercise. So the files for exercise two consists of a JavaScript file, HTML file, a CSS file, and an image. As with the first exercise, we're going to have you link the JavaScript file, today's tml file. That's good practice. And then what we want you to accomplish is to print a random number from zero to 10. and print it to the console. So little hint, you you're going to need to use the math random method.

But you'll also need to use another method from the math object. Now if you need a refresher on the math object, you can Google that and take a look at the resources w three c school is another good place for that. So go ahead and pause the video. Take a moment and see if you can figure this out and then once you're done, restart it and we'll go through this together. Okay, I hope you're able to accomplish that. Now just be aware there's probably more than while not probably there is more than one way to accomplish this.

So if yours looks slightly different, but you got the same results, then you should feel like you were successful. So first thing I'm going to do is create a num variable. And I'm going to assign a random number to it using the math object. Now I want to I want to manipulate this variable just a bit, but I'm going to do the results of that manipulation. I'm going to put it in a second variable, call it final num. I'm gonna set that equal to num and multiply that by 10 know why Multiply by 10.

Well, if you remember the random method or the math object returns a number between zero and one. If we want a random number between zero and 10, then we would need to multiply the result of random by 10. It's still going to give us a number with a decimal point. So we still need to do one more thing. But that at least gives us now something between zero and 10. Now, we could go ahead and manipulate this variable one more time, but I'm actually going to do that as a part of the console dot log statement.

That's why I say there's a couple different ways this could be done. So when caught inside console dot log, I'm going to use the round method to round the final number. And that's what we will print out. Go ahead and close that with a semi colon Now by rounding, it will be a number between zero and 10. It will no longer be a number with some decimal after it some decimal point value after it. Okay, so we've got what we need in app dot j s. I'm going to save that.

Now I'm going to go to my HTML file. And let's link by adding a script tag. Set the src attribute to App dot j s, because they're in the exact same folder. If they're in a different folder, then I'd have to use a path to point to that. Let's go ahead and save this. I'm going to now I'm going to copy the file path to it just to make it easier to open in a browser.

Go back to my browser. And let's open it up. Open the console and see what we got. So looks like we got a three. I'm going to dock the console to the bottom so that I can refresh this page and see the different numbers. I got 655 again, six again.

So it looks like it's working. I haven't gotten a number below zero and haven't gotten a number above 10 so that hopefully you were able to accomplish that and get the same results.

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.