### Thread: Age calculator , how to turn current year into a variable

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0

#### Age calculator , how to turn current year into a variable

I need to know how to turn the current year into a varible so I can employ it in my equation. I am creating a age calculator that gives the persons age based on birth year subttracted from the current year. I cannot get my equation to work.

Here is my assignment :
1.**Volume** Of a Box:Allow the user to enter the width, length, and height of a box. When they hit a button that says "View Volume" it multiplies all 3 numbers together and shows the result in a disabled input box. -
2.Age: Allow the user to enter their year of birth. When they hit a button that says "Age This Year" it takes the current year and subtracts their year of birth. Display the result in a disabled input box. -

Part one is perfect but part two I cannot get anything to display in my box

Code:
``` <title>Exercise 1</title>

<script type='text/javascript'>
function calculateSum()
{var a = parseInt(document.getElementById('firstNumber').value);
var b = parseInt(document.getElementById('secondNumber').value);
var sum = a*b;
document.getElementById('sumPlaceholder').value = sum;}</script>

<script type="text/javascript">
function calcualteAge()
{var a = parseInt(document.getElementById('birthyear').value);
var now = new Date();
var year =  now.getyear();
var age = year-a;
document.getElementById('agePlaceholder').value = age;}
</script>

<body>
<p>Please enter length and width below and I will give you the area.
<br />
Length <input type='text' id='firstNumber' /> <br />
Width <input type='text' id='secondNumber' /> <br />
Area: <input type='text' id='sumPlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateSum"
value="Display Sum" onclick='calculateSum()'/></p>

<br />
Year of Birth: <input type='text' id='birthyear' /> <br />
Current Year: <script type='text/javascript'>
var today = new Date();
document.writeln(today.getFullYear() );
</script> <br />

Age:<input type='text' id='agePlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateAge"
value="Display Age" onclick='calculateAge()'/></p>

</body>
</html>```
2. You had your "calcualteAge()" function spelled wrong; in the onclick event of your button. And... you was trying to subtract the end user's Y.O.B. from a complete date object; because you had the "getYear()" (which has been depreciated by the W3C) spelled wrong, as well. You just needed to change that to getFullYear(); like you had in the document.writeln().

Code:
``` <title>Exercise 1</title>

<script type='text/javascript'>
function calculateSum()
{
var a = parseInt(document.getElementById('firstNumber').value);
var b = parseInt(document.getElementById('secondNumber').value);
var sum = a*b;
document.getElementById('sumPlaceholder').value = sum;
}
function calcualteAge()
{
var a = parseInt(document.getElementById('birthyear').value);
var now = new Date();
var year =  now.getFullYear();
var age = year-a;
document.getElementById('agePlaceholder').value = age;
}
</script>

<body>
<p>Please enter length and width below and I will give you the area.
<br />
Length <input type='text' id='firstNumber' /> <br />
Width <input type='text' id='secondNumber' /> <br />
Area: <input type='text' id='sumPlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateSum"
value="Display Sum" onclick='calculateSum()'/></p>

<br />
Year of Birth: <input type='text' id='birthyear' /> <br />
Current Year: <script type='text/javascript'>
var today = new Date();
document.writeln(today.getFullYear());
</script> <br />

Age:<input type='text' id='agePlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateAge"
value="Display Age" onclick='calcualteAge()'/></p>

</body>
</html>```
Last edited by web_loone08; January 20th, 2013 at 09:56 PM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
2
Rep Power
0

#### Thanks but i think i am missing something else still

Thanks so much for your response. I have applied the changes but it still doesn't return anything in the answer box. Is there something else i am missing?

Code:
``` <title>Exercise 1</title>

<script type='text/javascript'>
function calculateSum()
{
var a = parseInt(document.getElementById('firstNumber').value);
var b = parseInt(document.getElementById('secondNumber').value);
var sum = a*b;
document.getElementById('sumPlaceholder').value = sum;
}
function calculateAge()
{
var a = parseInt(document.getElementById('birthyear').value);
var now = new Date();
var year =  now.getFullYear();
var age = year-a;
document.getElementById('agePlaceholder').value = age;
}
</script>

<body>
<p>Please enter length and width below and I will give you the area.
<br />
Length <input type='text' id='firstNumber' /> <br />
Width <input type='text' id='secondNumber' /> <br />
Area: <input type='text' id='sumPlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateSum"
value="Display Sum" onclick='calculateSum()'/></p>

<br />
Year of Birth: <input type='text' id='birthyear' /> <br />
Current Year: <script type='text/javascript'>
var today = new Date();
document.writeln(today.getFullYear());
</script> <br />

Age:<input type='text' id='agePlaceholder' disabled='disabled'/> <br />
<input type="button" id="calculateAge"
value="Display Age" onclick='calculateAge()'/></p>

</body>
</html>```
4. What's not working about it; look's like it's working fine to me?
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2011
Posts
33
Rep Power
7
It also appears to be working for me. Try using it in a different browser yours just might be acting up.

Although without using month/day as well as year it wont be very accurate, for example I was born in 1992 and it says I am 21 when Im actually only 20.