Now, let's build up on what we learned
up to this point.
We are calculating our adult years here
by deducting 18 from the age.
Now we can actually switch the order here
and do that before I reassign the age.
And then thereafter calculate the new adult years,
which is age minus 18, again.
So now I'm doing the same thing twice,
but I do it for the different age values
because, in between, I do assign a new age value.
Now I'm still only outputting adult years once
down there, though.
Hence, if I do that and save this,
if this reloads,
I still only see 27 because I never alert adult years
before I recalculated.
But of course we could do that.
We could also alert it here
and then reassign the age and recalculate adult years.
By the way, if you're wondering
why I'm sometimes adding an empty line,
this is totally optional, not required.
I'm just doing it for readability purposes
to make my code a bit easier to read.
You don't have to add to those blank lines.
But back to our code.
I did now also alert adult years here.
And hence, of course, we now see 14 and then 27.
Now, why am I showing this here?
Because, now we again have some code duplication.
We are repeating the same step in two different places.
And, do you see which step I'm talking about?
Which operation I'm talking about?
I'm talking about the calculation here,
where we are deriving adult years.
Now, of course, we are also repeating this alert code here,
for example, but there is an important difference.
For alert, we are just executing one command,
which is built into the browser.
For this operation here, for this calculation,
we basically built our own very simple algorithm.
We have a clearly defined operation
that executes certain steps.
In this case, one key step where we deduct 18 from age.
But if this operation would change,
if I, for some reason, need to add these values,
instead of subtract them,
then we have to do this in two places.
And we typically want to avoid scenarios like this
as a developer.
We want to be "lazy," as it's sometimes called.
We don't want to do the same work in different places,
because if we ever decide
that the correct operation would be
to use a plus here to add these two values,
then we have search our entire code
for every occurrences of this operation
and make the changes there as well,
which is annoying and error prone.
We can, for example,
easily overlook one of the places
where adjustments would be needed
if we have complex code.
That's why we typically want to put our own operations
into our own clearly defined commands,
which have their own name
and which we can then reuse
by just then using that name, so to say,
instead of repeating the entire operation code all the time.
So basically like it's working for the built in alert.
Here, we always repeat the same command,
which has the same name.
And adding errors here is way harder
because we get all the completion for that name and so on.
And hence, this is a great way of reusing logic.
The logic for outputting this alert box with some content.
Here, we now also want to write our own command
and that's called a function.