Jump to content

Help!


Mengao
 Share

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...