Question

In: Computer Science

Is there a way to tell the user in the alert how old they are based...

Is there a way to tell the user in the alert how old they are based on their birthday?

<html>

        <head>

        </head>

        <body style="background-color: lightgreen;">

                   <center><h1>Contact List</h1></center>

                <center>

                        <form>

                                <label>firstname: </label>

                                <input type="text" id="firstname">

                                <br/><br/>

                                <label>lastname:</label>

                                <input type="text" id="lastname">

                                <br/><br/>

                                <label>birthday:</label>

                                <input type="date" id="birthday">

                                <br/><br/>

                                <label>address:</label>

                                <textarea id="addr"></textarea>

                                <br/><br/>

                                <input type=submit onclick="datacalc()">

                        </form>

                </center>

        <script src="script.js">  

        </script>

        </body>

</html>

//function to call when form is submitted

function datacalc(){

    //creating and storing the form data into the person object

    var person = {

            //1. storing each value in respective data components

            firstname : document.getElementById('firstname').value,

            lastname : document.getElementById('lastname').value,

            //3. Storing the date value in birth propertyName

            birth : document.getElementById('birthday').value,

            addr : document.getElementById('addr').value,

            //2. A method to return greetings to the user

            greet : function() {

                    return "Hi "+this.firstname+"... Glad to meet you !\n";

                    }

    };

    //calling the greet function and accessing object values of person

    alert( person.greet() + "Your birthday is on " + person.birth );

    localStorage.setItem('p', JSON.stringify(person));   //to store the object after serializing it. 'p' is just the id to retrieve it back later. stringify() will convert it into serialized form.

  

    var newPerson = JSON.parse(localStorage.getItem('p'));   //to retrieve back the serialized object we stored using the id 'p' we assigned to it. parse() will convert it back into the original object.

    alert("Your name as stored in storage is " + newPerson.firstname + " " + newPerson.lastname + " and your birth date stored is " + newPerson.birth);

}

Solutions

Expert Solution

I have written there my contribution where I have edited the code.

       

<html>

        <head>
                <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
        </head>

        <body style="background-color: lightgreen;">

                   <center><h1>Contact List</h1></center>

                <center>

                        <form>

                                <label>firstname: </label>

                                <input type="text" id="firstname">

                                <br/><br/>

                                <label>lastname:</label>

                                <input type="text" id="lastname">

                                <br/><br/>

                                <label>birthday:</label>

                                <input type="date" id="birthday">

                                <br/><br/>

                                <label>address:</label>

                                <textarea id="addr"></textarea>

                                <br/><br/>

                                <input type=submit onclick="datacalc()">

                        </form>

                </center>

        <script>
        function datacalc(){

    //creating and storing the form data into the person object

    var person = {

            firstname : document.getElementById('firstname').value,

            lastname : document.getElementById('lastname').value,

            //3. Storing the date value in birth propertyName

            birth : document.getElementById('birthday').value,

            addr : document.getElementById('addr').value,

            //2. A method to return greetings to the user

            greet : function() {

                    return "Hi "+this.firstname+"... Glad to meet you !\n";

                    }

    };
    

    //calling the greet function and accessing object values of person
    var today = new Date();
        var dd = String(today.getDate()).padStart(2, '0');
        var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
        var yyyy = today.getFullYear();
        today = yyyy + '-' + mm + '-'+dd;

    localStorage.setItem('p', JSON.stringify(person));   //to store the object after serializing it. 'p' is just the id to retrieve it back later. stringify() will convert it into serialized form.

  

    var newPerson = JSON.parse(localStorage.getItem('p'));   //to retrieve back the serialized object we stored using the id 'p' we assigned to it. parse() will convert it back into the original object.
        









                // My Contribution needed a function to calculte the date


                
function diff_year_month_day(dt1, dt2) 
     {
      var time =(dt2.getTime() - dt1.getTime()) / 1000;
      var year  = Math.abs(Math.round((time/(60 * 60 * 24))/365.25));
      var month = Math.abs(Math.round(time/(60 * 60 * 24 * 7 * 4)));
      var days = Math.abs(Math.round(time/(3600 * 24)));
      return( "Year :- " + year + " Month :- " + month%12 + " Days :-" + days%31);
      
     }
        var today = new Date();
        var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
            dt1 = new Date(date);
    dt2 = new Date(newPerson.birth);

    alert("Your name as stored in storage is " + newPerson.firstname + " " + newPerson.lastname + " and your birth date stored is " + newPerson.birth+ "\n Age of Person is "+ diff_year_month_day(dt2, dt1));

}  


















        
        </script>

        </body>

</html>

   


Related Solutions

ask the user for the length and width of ywo rectangles. tell the user the area...
ask the user for the length and width of ywo rectangles. tell the user the area of the reftangle with the greater width. C++
Tell the story of how a proton created in the Big Bang made its way into...
Tell the story of how a proton created in the Big Bang made its way into your body, and is now a part of a carbon atom in your cell's DNA. Think of some the places where this atom might have lived over the years, and remember that atomic nuclei get transformed within stars and may be incorporated into future planets. Include at least 4 different stages of development including the processes that drove the transformations.
Best way to remember how to tell the difference between an ordinary annuity and annuity due...
Best way to remember how to tell the difference between an ordinary annuity and annuity due when doing a problem? Also is it easier to just remember the formulas instead of looking at the tables?
We talked in class about how some textbooks tell us that the way to conduct a...
We talked in class about how some textbooks tell us that the way to conduct a cost benefit analysisassociated with a policy is to look atthe costs and benefits beforea policy was put in place and compare them to the cost and benefits after the policy is in place. Is this correct? If so, explain why. If not, explain why not. NOTE: I realize that you should say "with" or "without" the policy, NOT "before" and "after" but I don't...
Create a JavaScript program of objects that asks the user how old they are in years....
Create a JavaScript program of objects that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use if/else conditional statements to display their approximate age in the selected timeframe. Perform all calculations using an AgeConverter class that accepts the age in years during initialization and has separate properties and methods to calculate and return the age in months, days, hours,...
How has the User Generated Content (UGC) altered the way that products are promoted in digital...
How has the User Generated Content (UGC) altered the way that products are promoted in digital world? Provide an example, explain your realizations and rationale.
Tell the what liability ratios mean. explain detailed way
Tell the what liability ratios mean. explain detailed way
Suppose after a high-smog alert day in your city, a 5-year-old child with asthma, a 50-year-old...
Suppose after a high-smog alert day in your city, a 5-year-old child with asthma, a 50-year-old male smoker with emphysema, and a 75-year-old of average health all come to the emergency room complaining of a cough and chest pain. Which patient’s health would you be the most concerned about and why? How do breathed-in air pollutants, such as smoke or asbestos, interfere with or worsen the respiration process in each of these scenarios?
Why? How could you tell a parent/patient/customer about attachment theory in a sensitive and respectful way?
Why? How could you tell a parent/patient/customer about attachment theory in a sensitive and respectful way?
Foamy Ltd. plans to produce a coffee-based drink called Alert. It is anticipated that variable costs...
Foamy Ltd. plans to produce a coffee-based drink called Alert. It is anticipated that variable costs will amount to 40p per drink. The company will produce Alert in a processing facility with a capacity to produce 200,000 drinks a year. Fixed costs are anticipated to be £100,000 per year. The company plans to supply to retailers at a price of 95p per drink. Required: a. Calculate the break-even volume, at the expected price, to retailers. b. Calculate the break-even sales...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT