Today's Java Script learning session was about completing my first small project.
Here's is the main function of my website.
In order to calculate your age in days, do the following:
Java Script:
function ageInDays () {
var actualYear = prompt('What is the actual year?');
var birthYear = prompt('What year were you born?');
var remainderYears = (actualYear - birthYear);
var normalDays = (remainderYears * 365);
var additionalDays = (Math.floor(remainderYears / 4));
var totalDays = (normalDays + additionalDays);
var h1 = document.createElement('h1');
var textAnswer = document.createTextNode('You are ' + totalDays + ' days old');
h1.setAttribute('id', 'ageInDays');
h1.appendChild(textAnswer);
document.getElementById('flex-box-result').appendChild(h1);
And in order to call this function:
HTML:
<body>
<div class = "container-1">
<h2>Challenge 1: Your age in days</h2>
<div class = "flex-box-container-1">
<div>
<button class = "btn btn-primary" onclick="ageInDays()">Click me</button>
</div>
<div>
<button class = "btn btn-danger" onclick="reset()">Reset</button>
</div>
</div>
<div class = "flex-box-container-1">
<div id = "flex-box-result"></div>
</div>
</div>
I'll upload the second challenge tomorrow.
It's a cat generator!
Dam, I really know how to spend my time.
Closing thoughts
- I know I should be using 'let' or 'const' instead of 'var' but I wasn't able to call my function while clicking the "ageInDays()" button. What changes would you recommend me doing?