JavaScript on the go.

Just started Javascript Tutorial Blogging.

Javascript Tutorial #1

~ Age Calculator ~

-Generates the age of a certain person in years and months.

-Given as the year as 2015 and the month as august.

<html>
<title>Calculate Age</title>
<script>
function birthdate(no)
{
var choose = document.getElementById(no);
var chosen = choose.value;
switch(chosen)
{
case ‘Calculate’:
var year = document.getElementById(“y1”).value;
var month = document.getElementById(“m1”).value;

var yes = 2015 – parseInt(year);
var yes1 = 8 – parseInt(month) ;

if( yes1 == -1)
{
yes = yes – 1;
yes1 = yes1 + 12 ;
document.write(yes + “years”);
document.write(yes1 + “months”);
if(yes < 20 && yes > 1)
{
document.write(“YOUNG”);
}
if(yes < 29 && yes > 21)
{
document.write(“TEENAGER”);
}
if(yes >30)
{
document.write(“OLD ENOUGH”);
}
}
if( yes1 == -2)
{
yes = yes – 1;
yes1 = yes1 + 12 ;
if(yes < 20 && yes > 1)
{
document.write(“YOUNG”);
}
if(yes < 29 && yes > 21)
{
document.write(“TEENAGER”);
}
if(yes >30)
{
document.write(“OLD ENOUGH”);
}
document.write(yes + “years”);
document.write(yes1 + “months”);
}
if( yes1 == -3)
{
yes = yes – 1;
yes1 = yes1 + 12 ;
if(yes < 20 && yes > 1)
{
document.write(“YOUNG”);
}
if(yes < 29 && yes > 21)
{
document.write(“TEENAGER”);
}
if(yes >30)
{
document.write(“OLD ENOUGH”);
}
document.write(yes + “years”);
document.write(yes1 + “months”);
}
if( yes1 == -4)
{
yes = yes – 1;
yes1 = yes1 + 12;
if(yes < 20 && yes > 1)
{
document.write(“YOUNG”);
}
if(yes < 29 && yes > 21)
{
document.write(“TEENAGER”);
}
if(yes >30)
{
document.write(“OLD ENOUGH”);
}
document.write(yes + “years”);
document.write(yes1 + “months”);
}
else
{
document.write(yes + “years”);
document.write(yes1 + “months”);
if(yes < 20 && yes > 1)
{
document.write(“YOUNG”);
}
if(yes < 29 && yes > 21)
{
document.write(“TEENAGER”);
}
if(yes >30)
{
document.write(“OLD ENOUGH”);
}
}
break;
}
}
</script>

<select id = “no”>
<option value = “Calculate”>Calculate</option>
</select>
<input type = ” text ” id = “m1” name = “month” placeholder = “Enter Birthmonth”>
<input type = ” text ” id = “d1” name = “day” placeholder = “Enter Birthday”>
<input type = ” text ” id = “y1” name = “year” placeholder = “Enter Birthyear”>
<input type = “button” value = “Calculate Age” onclick = “birthdate(‘no’)”>

</html>

 

 

I hope some of you might see these as a useful project reference. More tutorials about javascript.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s