OK so to make this age calculator I have a simple calc that each time l file was empty.
I'll fill it in with my standard boiler plate.
Let's put age calc as the title.
Let's put a little H-1 again that says
age calc solution and save it.
Next we're going to make our javascript file which I will call calc.
And I'm going to put a alert in there just to make sure it's connected and have my files connected OK
.
I go to my calc h t m l and I'll add in my script tag with source equals calc that genius.
And there's one thing I want to show you here.
If I save this and I go open it in the browser.
Notice that I don't see my H-1 yet.
So it tells me javascript alert connected but my H-1 hasn't loaded as soon as I click OK.
Then it has loaded.
And the reason that's happening is that I'm putting my script before this HDMI actually loads.
So if I wanted that each one to be there I could put the script afterwards and we will talk a lot more
about where you should put scripts.
Some of them should go in the head.
Some of them should go in the body.
In this case I'm going to put this one down here in the body.
It doesn't really make a big difference but it is worth knowing that if you put this script up top it's
going to run first.
So save this.
Now let's go finish our logic here.
So let's start by asking the user for their age so let's do var age because prompt.
What is your age save.
And then just to make sure it works.
Let's do this to an alert just age and that's it.
Then let's go back and open the file up.
What is your age.
10.
And then just print out 10 an alert.
OK.
So next up we're now going to go and add the logic in to figure out how many days someone has spent
a life.
So I'm going to make a variable to store that in and I'm going to just call it days.
So far days equals age times 365 days in a year and save that and then we'll do our alert here.
So lower.
And then we wanted to say age plus years is roughly plus the number days for us the word days.
So this should say whatever the ages years is roughly whatever the days is days.
So let's test that out.
Refresh What is your age.
Three three years is roughly one thousand ninety five days.
One small adaptation we can make is that this doesn't account for leap years.
So there is a leap year every four years.
So the easiest way to include that is just to average that out.
So instead of multiplying by 365 you multiply by 365 point to five a quarter of a year.
Now if we try this again let's say that we are 50.
We now get 50 years is roughly eighteen thousand two hundred sixty two point five days.
OK.
So that's all we had to do here.
So the only real difference between this and the stocker problem is that we're doing some math beforehand