Caters
Caters

Reputation: 147

Prompt method not working?

My javascript seems to be fine(.prompt() and everything, even the start of a nested if/else statement) and yet when I validate my javascript it says "Expected semicolon before statement" for the very first prompt.

I don't see how my code is wrong. I tried using an equal sign before the prompt to define the variables because I thought that if I didn't do that my variables would be undefined. I can't say for sure how many variables I will have so I can't just declare my list of variables ahead of time. This is with my character personality test.

Here is the code. The gender variable is supposed to only accept 2 answers, Male and Female. The childrenh variable is supposed to accept any number between 0 and 12(12 being the maximum per pregnancy for my characters). This is where an array might come in handy or some equivalent because then I could have cases leading to more .prompt() statements(Essentially the equivalent of 12 if/else statements)

function Character() {
  var gender.prompt("What is your gender?");
  if (gender = "Female") {

    var children_h.prompt("Have you had any children recently?");
    if (children_h = "0") {
      var pregnant.prompt("Are you pregnant?");
      if (pregnant = "Yes") {
        var pregnant_m.prompt("Is it a multiple pregnancy?");
        if (pregnant_m = "Yes") {
          var pregnant_t.prompt("How far are you in your pregnancy(trimester)?");
          if (pregnant_t = "1") {
            alert("Expect to feel like you are sick");
          };
          else if (pregnant_t = "2") {
            alert("You might feel some movements");
          };
          else if (pregnant_t = "3") {
            alert("You might feel pain before labor.")
          };
          else {
            alert("Your baby is more than ready. Don't be surprised if you go into labor.")
          };
        };
      };
    };
    else if (children_h = "1") {
      var children_a.prompt("How old is your child?")
    };
    else if (children_h = "2") {
      var chilren_m.prompt("Were they twins or not?")
    };
    else if (children_h = "3") {
      children_m.prompt("Were they twins or triplets?")
    };
    else if (children_h = "4") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "5") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "6") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "7") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "8") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "9") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "10") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "11") {
      children_m.prompt("Were they multiples?")
    };
    else if (children_h = "12") {
      children_m.prompt("Were they multiples?")
    };
    else {
      alert("Impossible!")
    };
  };
  else if (gender = "Male") {
    childrenh.prompt("Have you had any children recently?");
  };
  else {
    alert("Undefined gender!")
  };
};
<!DOCTYPE html>
<html>

<head>
  <link rel='stylesheet' href='style.css' />
  <script src='script.js'></script>
</head>

<body>
  <p>Take the personality test to find out your character's personality</p>
  <button onclick="Character">Test</button>
</body>

</html>

So why am I getting a syntax error with my variables? I declare those variables in the same lines that I declare my prompt statements.

EDIT:

I edited the code to include more if/else statements and a global function to connect to the HTML via the button tag. It still isn't working(Character isn't defined comes up when you click the button(Even though I have defined the function as all this if/else business and put it in by the same name in the HTML) and expected ; comes up when the program starts(Which I don't understand what is wrong with that particular else if statement)). I don't have any CSS code yet.

I am using Codecademy for this and it has built in external CSS and JS for all their codebits(That is why the script tag is in the header is because with it there, it should refer to the external javascript whenever it is called for(Like in this button that should lead to all these prompts and more when I am finished with it)).

Upvotes: 0

Views: 1260

Answers (2)

wdfc
wdfc

Reputation: 374

Please post your full code. I believe your problem is that you are not using a function for your variables.

Check out the wc3 example http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt

Upvotes: 1

Lumi Lu
Lumi Lu

Reputation: 3305

Change from

    var gender.prompt("What is your gender?");
if(gender = "Female"){

   var childrenh.prompt("Have you had any children recently");
};

to

var gender = prompt("What is your gender?");
if(gender === "Female"){        
   var children = prompt("Have you had any children recently");
};

Upvotes: 1

Related Questions