Javascript Issue

compwebnut777

Baseband Member
Messages
28
Hey guys, having a bit of a problem with some Javascript I wrote for class. When it runs, it fails, as in white screen instead of a prompt box. Anyone know what's going on? I'll put the code in real quick:

Code:
<html>
<head>

</head>
<body>

<script type="text/javascript">

alert("hi");


var birthmonth = parseInt(prompt("Please enter your month of birth",""));

if(birthmonth == 11){
   birthmonth = 2;
}

var birthdate = parseInt(prompt("Please enter your date of birth",""));

if(birthdate == 14){
   birthdate = 5;
}

var birthyear = parseInt(prompt("Please enter your year of birth",""));

if(birthyear == 1989}{
   birthyear = 9;
}


document.write(birthmonth);
document.write(birthdate);
document.write(birthyear);

</script>

</body>
</html>
What I'm trying to do is get the numbers 2, 5 and 9 to show up on the screen. I'm new to this (hence the probably basic code) and I don't have a clue what is going on, so any help would be greatly appreciated :).
 

DJ Stephen

Fully Optimized
Messages
3,275
I think it might becuase becuase you are setting the var as the prompt, then setting it as an integer. maybe a different name?
 

Daeva

In Runtime
Messages
407
Try this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us" dir="ltr">
 <head>
  <meta http-equiv="Content-Type" Content="text/html; charset=utf-8" />
  <title>Javascript Test</title>
  <script type="text/javascript">
   <!--

   //Declare variables//
    var bMonth = 0;
    var bDate = 0;
    var bYear = 0;
    var outputMonth = 0;
    var outputDate = 0;
    var outputYear =0;
   //End Declare Variables//

   //Get Values//
    bMonth = prompt("Please enter your Month of birth.","");
    bDate = prompt("Please enter your Date of birth.","");
    bYear = prompt("Please enter your Year of birth.","");
   //End Get Values//

   //Validate values//
    if(Number(bMonth).toString() == "NaN") {
      document.write("That is not a valid Month.<br />Please re-load the page and try again.");
    } else if(Number(bMonth) == 0) {
      document.write("You must enter a birth Month.<br />Please re-load the page and try again.");
    } else if(Number(bMonth) == 11) {
      outputMonth = 2;
      document.write(outputMonth);
    } else {
      document.write("Unknown Error on birth Month.<br />Please re-load the page and try again.<br />");
    } // end if/else

    if(Number(bDate).toString() == "NaN") {
      document.write("That is not a valid Date.<br />Please re-load the page and try again.<br />");
    } else if(Number(bDate) == 0) {
      document.write("You must enter a birth Date.<br />Please re-load the page and try again.<br />");
    } else if (Number(bDate) == 14) {
      outputDate = 5;
      document.write(outputDate);
    } else {
      document.write("Unknown Error on birth Date.<br />Please re-load and try again.<br />");
    } // end if/else

    if(Number(bYear).toString() == "NaN") {
      document.write("That is not a valid Year.<br />Please re-load the page and try agian.<br />");
    } else if(Number(bYear) == 0) {
      document.write("You must enter a valid birth Year.<br />Please re-load the page and try again.<br />");
    } else if(Number(bYear) == 1989) {
      outputYear = 9;
      document.write(outputYear);
    } else {
      document.write("Unknown Error on birth Year.<br />Please re-load the page and try again.<br />");
    } // end if/else
   //End Validate Values//

   //-->
  </script>
 </head>
 <body>
 </body>
 </html>
 

ksb007

Baseband Member
Messages
95
It looks like you may have a typo. You have a } where you need a ).

It happens in the following lines:

Code:
if(birthyear == 1989}{
   birthyear = 9;
}
It should be:

Code:
if(birthyear == 1989){
   birthyear = 9;
}
 

carolroy52

Beta member
Messages
1
Hi there

Have just found this site and I'm in London. Am doing some computing studies with the Open University and have bitten off more than I can chew!!! Can anyone tell me how to declare a variable in Javascript when the quantities are unknown, e.g. the number of runners in a race?

Many thanks and best wishes
Carol Roy
 
Top