Skip to content

Instantly share code, notes, and snippets.

@RobAWilkinson
Last active August 29, 2015 14:10
Show Gist options
  • Save RobAWilkinson/02c204cf593fff9a517e to your computer and use it in GitHub Desktop.
Save RobAWilkinson/02c204cf593fff9a517e to your computer and use it in GitHub Desktop.
Sample JS

Good code for age check

var today = new Date();
var todayYear = today.getFullYear(), 		
    todayMonth = (today.getMonth() + 1),  	
    todayDate = today.getDate();
    
var birthdayYear = prompt("What year were you born?"),
	birthdayMonth = prompt("What month"),
	birthdayDate = prompt("What Day were you born?");

if ((todayYear - birthdayYear) > 21) {
  console.log(21);
}
else if ( todayMonth > birthdayMonth){
  console.log(21);
}
else if (todayDate > birthdayDate) {
  console.log(21);
}
else if ((todayYear - birthdayYear) == 21 && (todayMonth == birthdayMonth) && (todayDate == birthdayDate)){
  console.log("HAPPY BDAY");
}
else {
  console.log("too young");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment