Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
Now it's time to practice everything 2
2
that you just learned about ES6 classes, 3
3
and so let's now do coding challenge number two, 4
4
and the goal of this one 5
5
is to basically recreate challenge number one, 6
6
but this time using an ES6 class, 7
7
then besides that, 8
8
I also want you to add a getter, 9
9
which should be called speedUS, 10
10
and this one should return the current speed 11
11
in miles per hour. 12
12
So remember that the original speed 13
13
is in kilometers per hour, 14
14
and people in the US are usually not used to that, 15
15
and so we can create a special getter called speedUS, 16
16
and to do the conversion from kilometers per hour 17
17
to miles per hour, all you need to do is to divide by 1.6, 18
18
then you should also implement a setter 19
19
with the same name of speedUS 20
20
so that people can basically set the speed 21
21
also in miles per hour, 22
22
but of course, that value should then be stored in the class 23
23
as kilometers per hour, 24
24
and so before storing the value, 25
25
you need to multiply it by 1.6, 26
26
so to convert it back to kilometers per hour, 27
27
and then take this test data, number one here, 28
28
and create a new car and experiment 29
29
with all of the methods and getters and setters. 30
30
So have some fun with this one, 31
31
and I'll see you back here once you are ready. 32
32
Okay. 33
33
So hopefully you did that without any problems 34
34
and you don't even need my solution, 35
35
but of course, as always, 36
36
I will still show it to you, 37
37
and what I'm gonna do is to simply copy the code 38
38
that we wrote earlier, 39
39
and I will then convert it to a class 40
40
because that's actually not that hard. 41
41
So here, we just need to call it a class 42
42
and let me call it cl again for class, 43
43
and okay. 44
44
So then the curly braces, 45
45
so one in the beginning, one in the end, 46
46
then this function is actually called the constructor, 47
47
and the rest is the same. 48
48
Here we don't need these, don't need these, 49
49
and here we don't need the prototype, 50
50
so that's the whole point of using classes, 51
51
and then we don't need this function, 52
52
and besides that, that's actually it. 53
53
So that's the constructor function 54
54
plus the methods that we added to its prototype property 55
55
simply converted to a class. 56
56
And all right, so that was pretty easy. 57
57
Now let's talk about the speedUS getter and setter. 58
58
So first up the getter, which is easier. 59
59
So speedUS and this one does not need any input 60
60
and it will simply return this.speed 61
61
and then divide it by 1.6, 62
62
as I instructed up there in the text. 63
63
Alright, and let's already test this now, 64
64
and the test data here is Ford 65
65
going at 120 kilometers per hour. 66
66
So that's a new CarCl 67
67
with the make of Ford 68
68
and a speed of 120. 69
69
So let's accelerate here a little bit, 70
70
but before we do that, 71
71
let's actually also read the speed in the US, 72
72
because I actually know that 120 is 75 miles per hour, 73
73
and so then I can know if this actually worked, 74
74
and this one here is simply returns the value, 75
75
and so we need to log it to the console. 76
76
And all right, 77
77
so indeed this 75 here coming from line 270 78
78
is this speedUS property, 79
79
So again, we basically transformed a method here 80
80
into a property 81
81
so that we read like this by using a getter, 82
82
then we accelerate a little bit, so we increase to 130, 83
83
and so that means that these methods, they keep working, 84
84
and so our conversion to a class a works flawlessly. 85
85
And now to finish, 86
86
let's just implement the setter here as well. 87
87
So set speedUS, 88
88
and then remember we always need 89
89
to take exactly one argument, 90
90
so that's gonna be the speed, of course, 91
91
and so here, we want to set the speed, 92
92
but of course, in kilometers per hour, 93
93
and so we take the speed and multiply it by 1.6, 94
94
and so now if we do ford.speed 95
95
equals 50 miles per hour, 96
96
then let's take a look at the ford object, 97
97
and here of course, it needs to be US. 98
98
All right, and so here we set the speed to 50, 99
99
and then what we get here is 80, 100
100
and so 80 is indeed 50 times 1.6. 101
101
And so I think that's actually it. 102
102
And yeah. 103
103
So challenge number two completed.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.