Mengao Posted December 29, 2006 Share Posted December 29, 2006 I was making a simple code that you put the year you were born and it gives you your age; but it didn't work. Can you please tell me where am I wrong?Thanks.Here is the code: <html><head><script type="text/javascript">function disp_prompt() { var year=prompt("Please write the year you were born","1950") var age=2006-year { document.write("Hello, you are" + age + "years old"!) } }</script></head><body><input type="button" onclick="disp_prompt()" value="Click here" /></body></html> Link to comment Share on other sites More sharing options...
xbl1 Posted December 29, 2006 Share Posted December 29, 2006 I was making a simple code that you put the year you were born and it gives you your age; but it didn't work. Can you please tell me where am I wrong?Thanks.Here is the code:<html><head><script type="text/javascript">function disp_prompt() { var year=prompt("Please write the year you were born","1950") var age=2006-year { document.write("Hello, you are" + age + "years old"!) } }</script></head><body><input type="button" onclick="disp_prompt()" value="Click here" /></body></html> change the "document.write("Hello, you are" + age + "years old"!)" to the document.write("Hello, you are" + age + "years old!"), then it must work. Link to comment Share on other sites More sharing options...
Mengao Posted December 29, 2006 Author Share Posted December 29, 2006 change the "document.write("Hello, you are" + age + "years old"!)" to the document.write("Hello, you are" + age + "years old!"), then it must work.Is not working :-( Link to comment Share on other sites More sharing options...
Procat Posted December 29, 2006 Share Posted December 29, 2006 Is not working :-(Try:<html><head><script type="text/javascript">function disp_prompt() { var year=prompt('Please write the year you were born','1950'); var age=2006-year; document.write('Hello, you are' + age + 'years old!'); }</script></head><body><input type="button" onclick="disp_prompt()" value="Click here" /></body></html> Link to comment Share on other sites More sharing options...
jesh Posted December 29, 2006 Share Posted December 29, 2006 Or try using parseInt() to make sure that javascript is treating the input as a number: <html><head><script type="text/javascript">function disp_prompt() { var year=prompt('Please write the year you were born','1950'); var age=2006 - parseInt(year); document.write('Hello, you are ' + age + ' years old!'); }</script></head><body><input type="button" onclick="disp_prompt()" value="Click here" /></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now