Now it's time to practice everything
that you just learned about ES6 classes,
and so let's now do coding challenge number two,
and the goal of this one
is to basically recreate challenge number one,
but this time using an ES6 class,
then besides that,
I also want you to add a getter,
which should be called speedUS,
and this one should return the current speed
in miles per hour.
So remember that the original speed
is in kilometers per hour,
and people in the US are usually not used to that,
and so we can create a special getter called speedUS,
and to do the conversion from kilometers per hour
to miles per hour, all you need to do is to divide by 1.6,
then you should also implement a setter
with the same name of speedUS
so that people can basically set the speed
also in miles per hour,
but of course, that value should then be stored in the class
as kilometers per hour,
and so before storing the value,
you need to multiply it by 1.6,
so to convert it back to kilometers per hour,
and then take this test data, number one here,
and create a new car and experiment
with all of the methods and getters and setters.
So have some fun with this one,
and I'll see you back here once you are ready.
Okay.
So hopefully you did that without any problems
and you don't even need my solution,
but of course, as always,
I will still show it to you,
and what I'm gonna do is to simply copy the code
that we wrote earlier,
and I will then convert it to a class
because that's actually not that hard.
So here, we just need to call it a class
and let me call it cl again for class,
and okay.
So then the curly braces,
so one in the beginning, one in the end,
then this function is actually called the constructor,
and the rest is the same.
Here we don't need these, don't need these,
and here we don't need the prototype,
so that's the whole point of using classes,
and then we don't need this function,
and besides that, that's actually it.
So that's the constructor function
plus the methods that we added to its prototype property
simply converted to a class.
And all right, so that was pretty easy.
Now let's talk about the speedUS getter and setter.
So first up the getter, which is easier.
So speedUS and this one does not need any input
and it will simply return this.speed
and then divide it by 1.6,
as I instructed up there in the text.
Alright, and let's already test this now,
and the test data here is Ford
going at 120 kilometers per hour.
So that's a new CarCl
with the make of Ford
and a speed of 120.
So let's accelerate here a little bit,
but before we do that,
let's actually also read the speed in the US,
because I actually know that 120 is 75 miles per hour,
and so then I can know if this actually worked,
and this one here is simply returns the value,
and so we need to log it to the console.
And all right,
so indeed this 75 here coming from line 270
is this speedUS property,
So again, we basically transformed a method here
into a property
so that we read like this by using a getter,
then we accelerate a little bit, so we increase to 130,
and so that means that these methods, they keep working,
and so our conversion to a class a works flawlessly.
And now to finish,
let's just implement the setter here as well.
So set speedUS,
and then remember we always need
to take exactly one argument,
so that's gonna be the speed, of course,
and so here, we want to set the speed,
but of course, in kilometers per hour,
and so we take the speed and multiply it by 1.6,
and so now if we do ford.speed
equals 50 miles per hour,
then let's take a look at the ford object,
and here of course, it needs to be US.
All right, and so here we set the speed to 50,
and then what we get here is 80,
and so 80 is indeed 50 times 1.6.
And so I think that's actually it.
And yeah.
So challenge number two completed.