javascript - 提示方法不起作用?

标签 javascript global-variables prompt local-variables

我的 javascript 似乎很好(.prompt() 和所有内容,甚至是嵌套 if/else 语句的开头),但是当我验证我的 javascript 时,它在第一个提示中显示“语句前预期分号”。

我不明白我的代码有什么问题。我尝试在提示之前使用等号来定义变量,因为我认为如果不这样做,我的变量将是未定义的。我不能确定我将有多少变量,所以我不能提前声明我的变量列表。这是我的性格测试。

这是代码。性别变量应该只接受 2 个答案:男性和女性。 childh 变量应该接受 0 到 12 之间的任何数字(12 是我的 Angular 色每次怀孕的最大值)。这就是数组或类似的东西可能派上用场的地方,因为这样我可能会遇到导致更多 .prompt() 语句的情况(基本上相当于 12 个 if/else 语句)

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>

那么为什么我的变量会出现语法错误?我在声明提示语句的同一行中声明这些变量。

编辑:

我编辑了代码以包含更多 if/else 语句以及通过按钮标记连接到 HTML 的全局函数。它仍然无法正常工作(当您单击按钮时,会出现“字符未定义”(即使我已将函数定义为所有这些 if/else 业务,并在 HTML 中以相同的名称将其放入)并且预期 ; 出现程序启动时启动(我不明白那个特定的 else if 语句有什么问题))。我还没有任何 CSS 代码。

我为此使用 Codecademy,它为所有代码位内置了外部 CSS 和 JS(这就是为什么 script 标签位于 header 中,因为有了它,无论何时调用它,它都应该引用外部 javascript for(就像这个按钮一样,当我完成它时,应该会导致所有这些提示以及更多))。

最佳答案

更改自

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

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

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

关于javascript - 提示方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626405/

相关文章:

javascript - node.js 标准模块的全局变量?

r - 将 R Shiny 应用程序保存为函数,并将参数传递给 Shiny 的应用程序

go - 文件专用的全局变量

php - 服务器端登录提示框

Javascript:不知道为什么这个解决方案有效

javascript - 使用 React 在同一文档中添加新字段作为数组,而无需覆盖 Firestore 中的旧字段

javascript - 我可以从剪贴板中截取屏幕截图吗?

javascript - 结构化 JSON 项的正则表达式匹配

javascript - 将 javascript "strings"连接到对象中

authentication - 防止交互式 ssh 提示