Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com calculate age from year of birth

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-21-2013, 02:45 AM PM User | #1 GypsyParrot New to the CF scene   Join Date: Jan 2013 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts calculate age from year of birth I have to create a javascript program that calculates age based soely on year of birth subtracted from the current year. I am having issues figuring out how to convert the current year into a variable so i can use it in my function for calculation. Can some one tell me what i am doing wrong. Here is the assignment and i have already completed all but the calculation for the age calculator: 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. - 50 points 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. - 50 points Exercise 1

Please enter length and width below and I will give you the area.
Length
Width
Area:

Year of Birth:
Current Year:
Age:

 01-21-2013, 05:46 AM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,962 Thanks: 75 Thanked 4,309 Times in 4,276 Posts How much will you KICK yourself? Code: ```function calcualteAge() ... ``` By the by: It's not a great idea to use a function with the same name as some object's ID. It can make later debugging much more difficult, when the browser sees the ID and treats it as a JS name. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 01-21-2013, 05:50 AM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,962 Thanks: 75 Thanked 4,309 Times in 4,276 Posts p.s.: If you will learn to use a JavaScript debugger, you will be able to find stuff like this yourself in a matter of seconds, minutes at most. You can use the JS debugger in any browser, but I personally feel that the one in CHROME browser is the easiest to learn and easiest to use. Just bring up your web page in Chrome and then hit the F12 key. And then click on the "Console" tab to see your errors. And then take the time to learn what you can do after you click on the "Source" tab. __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 01-21-2013, 08:23 AM PM User | #4 Airblader Regular Coder   Join Date: Jan 2013 Location: Germany Posts: 578 Thanks: 4 Thanked 77 Times in 77 Posts Also by the way: You cannot reliably calculate the age based on year information only. I was born 1990 – am I 22 or 23?
 01-21-2013, 08:59 AM PM User | #6 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,099 Thanks: 0 Thanked 579 Times in 569 Posts To correctly calculate someone's age using their date of birth (and validating that they entered it correctly in mm/dd/ccyy format) Code: ```Date.prototype.ageLastBirthday = function(dob) { var cy, by, db, adj; cy = this.getFullYear(); by = dob.getFullYear(); db = new Date(dob); db.setFullYear(cy); adj = (this-db<0) ? 1 : 0; return cy - by - adj; }; validateDate = function(y,m,d) { var dt; if (d !== +d || m !== +m || y !== +y) return false; dt = new Date(y, --m, d); if (d !== dt.getDate() || m !== dt.getMonth() || y !== dt.getFullYear()) return false; return dt; } var db = document.getElementById('dateOfBirth').value.split('/'); if (validateDate(d[2],d[0],d[1]) { var today = new Date(); var dob = new Date(d[2],--d[0],d[1]); document.getElementById('agePlaceholder').value = today.ageLastBirthday(dob); } else document.getElementById('agePlaceholder').value = 'Invalid date of birth entered';``` __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers. Last edited by felgall; 01-21-2013 at 09:12 AM..

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 12:33 PM.