Coding Journal #5 - I'll Tell You Your Age in Days

Coding Journal #5 - I'll Tell You Your Age in Days

By Edward Casanova | Coding Journal | 9 Jun 2020


bdf214cb1176e1451e66d0abc73f6a7cadb0e90c80cbe77e4af5e5f6d551097d.jpeg

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?

Crypto Stuff

How do you rate this article?

3


Edward Casanova
Edward Casanova

🔹Welcome To My Coding Journey 🔹https://cointr.ee/edca3899


Coding Journal
Coding Journal

🔹Welcome To My Coding Journey 🔹https://cointr.ee/edca3899

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.