嵌套的JavaScript函数,或者While()循环

嵌套的JavaScript函数,或者While()循环,javascript,Javascript,我知道关于这个话题已经有两个问题发布了,但它们并没有真正回答我的问题(或者是因为我只是一个低水平的程序员)。我的问题是,我正在尝试通过实践来学习java(并且失败了很多次),所以我正在尝试制作一个游戏。我输入了大量的代码,它开始看起来很糟糕,所以我想oO(为什么不嵌套一些while()语句,这样可以压缩代码!)。。。它不起作用,我不明白为什么 $(document).ready(function() { $("#console").fadeIn(3000); //A c

我知道关于这个话题已经有两个问题发布了,但它们并没有真正回答我的问题(或者是因为我只是一个低水平的程序员)。我的问题是,我正在尝试通过实践来学习java(并且失败了很多次),所以我正在尝试制作一个游戏。我输入了大量的代码,它开始看起来很糟糕,所以我想oO(为什么不嵌套一些while()语句,这样可以压缩代码!)。。。它不起作用,我不明白为什么

    $(document).ready(function() {

        $("#console").fadeIn(3000); //A console to keep everything good

        $("form").submit(function() { //User input
            var input = $("#command_line").val(); //A nifty box to put your answers in
            var check = false;
            function check() { //If you don't follow directions this happens
                check = true;
            };
        //startup
        function start() { //This function is nested, and runs
            var yes = false; //defining yes
            var no = false; //defining no
            while (yes == false && no == false && currentarea == "startup") { //<-- This while loop does nothing?
                if (input == "yes") {
                    yes = true; //changing yes to true
                    $("<p>Great! What is your gender? Type <u><b>m</b></u> for Male or <u><b>f</b></u> for Female.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                    check();
                }
                else if (input == "no") {
                    no = true; //changing no to true
                    $("<p>Sorry to see you go. Press F5 to restart, or exit your browser.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                    check();
                }
                else if (yes == true || no == true) {   
                    $("<p>You've already answered this question.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                    check();
                }
            }   
        };
$(文档).ready(函数(){
$(“#控制台”).fadeIn(3000);//保持一切正常的控制台
$(“表单”).submit(函数(){//用户输入
var input=$(“#命令行”).val();//一个漂亮的框,可以把答案放进去
var检查=假;
函数检查()
检查=正确;
};
//启动
函数start(){//此函数嵌套并运行
var yes=false;//定义yes
var no=false;//定义no

而(yes==false&&no==false&¤tarea==“startup”){/正如@Ankur所指出的,您从未调用
start
函数

您可以这样做:

  $(document).ready(function() {
      $("#console").fadeIn(3000); //A console to keep everything good

      $("form").submit(function() { //User input
          var input = $("#command_line").val(); //A nifty box to put your answers in
          var check = false;

          function check() { //If you don't follow directions this happens
              check = true;
          };
          //startup
          function start() { //This function is nested, and runs
              var yes = false; //defining yes
              var no = false; //defining no
              while (yes == false && no == false && currentarea == "startup") { //<-- This while loop does nothing?
                  if (input == "yes") {
                      yes = true; //changing yes to true
                      $("<p>Great! What is your gender? Type <u><b>m</b></u> for Male or <u><b>f</b></u> for Female.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  } else if (input == "no") {
                      no = true; //changing no to true
                      $("<p>Sorry to see you go. Press F5 to restart, or exit your browser.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  } else if (yes == true || no == true) {
                      $("<p>You've already answered this question.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  }
              }
          };
          start();
      });
  });
$(文档).ready(函数(){
$(“#控制台”).fadeIn(3000);//保持一切正常的控制台
$(“表单”).submit(函数(){//用户输入
var input=$(“#命令行”).val();//一个漂亮的框,可以把答案放进去
var检查=假;
函数检查()
检查=正确;
};
//启动
函数start(){//此函数嵌套并运行
var yes=false;//定义yes
var no=false;//定义no

while(yes==false&&no==false&¤tarea==“启动”){//这是javascript而不是Java您甚至没有调用
start
函数。@Manos Kounelakis-很抱歉。我太新了,不知道两者之间的区别。我应该在挂上标签之前进行调查。@Ankur-我明白了。我甚至没有正确地执行循环。所以,我真的浪费了您的时间……很抱歉t、 谢谢。它允许循环执行我想要的操作。对于第二部分,“也不要在事件中声明新函数(如表单提交)。在代码顶部声明它”我在关注YouTube视频。这也是我的代码非常长的原因。
    //I had these variables already, but never posted them.
    var yes = false;
    var no = false;
    currentarea = "Start";
    //I added some do->if structures that I didn't have before.
    function start() {
        while(yes != true && no != true && currentarea == "Start") {
            if (input == "yes") {
                yes = true;
                $("<p>Great! What is your gender? Type <u><b>m</b></u> for Male or <u><b>f</b></u> for Female.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                currentarea = "Gender Assignment";  //<--This is what I had to put to get to next function. Easy enough... now that I know. Lol.
                check();
            }
            else if (input == "no") {
                no = true;
                $("<p>Sorry to see you go. Press F5 to restart, or exit your browser.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                check();
            }
        }  //<--I also noticed that the if->true statement was stupid, especially when I was telling it where to proceed to.
    }
    start();

    function gender_assignment() {
        while (male != true && female != true && currentarea == "Gender Assignment") {
            if (input == "m") {
                male = true;
                gender = "Male";
                HP = m_hp;
                document.getElementById("HP").innerHTML = HP;
                MP = m_mp;
                document.getElementById("MP").innerHTML = MP;
                Attack = m_attack;
                document.getElementById("Attack").innerHTML = Attack;
                Endur = m_endur;
                document.getElementById("Endur").innerHTML = Endur;
                Blk = m_blk;
                document.getElementById("Blk").innerHTML = Blk;
                currentarea = "Name";
                document.getElementById("gender").innerHTML = gender;
                $("<p>Nice to meet cha, bro. What's your name?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                check();
            }
            else if (input == "f") {
                female = true;
                gender = "Female";
                HP = f_hp;
                document.getElementById("HP").innerHTML = HP;
                MP = f_mp;
                document.getElementById("MP").innerHTML = MP;
                Attack = f_attack;
                document.getElementById("Attack").innerHTML = Attack;
                Endur = f_endur;
                document.getElementById("Endur").innerHTML = Endur;
                Blk = f_blk;
                document.getElementById("Blk").innerHTML = Blk;
                currentarea = "Name";
                document.getElementById("gender").innerHTML = gender;
                $("<p>Sup, Girl? What's your name?</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                check();
            }
        }
    }
    gender_addignment();
  $(document).ready(function() {
      $("#console").fadeIn(3000); //A console to keep everything good

      $("form").submit(function() { //User input
          var input = $("#command_line").val(); //A nifty box to put your answers in
          var check = false;

          function check() { //If you don't follow directions this happens
              check = true;
          };
          //startup
          function start() { //This function is nested, and runs
              var yes = false; //defining yes
              var no = false; //defining no
              while (yes == false && no == false && currentarea == "startup") { //<-- This while loop does nothing?
                  if (input == "yes") {
                      yes = true; //changing yes to true
                      $("<p>Great! What is your gender? Type <u><b>m</b></u> for Male or <u><b>f</b></u> for Female.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  } else if (input == "no") {
                      no = true; //changing no to true
                      $("<p>Sorry to see you go. Press F5 to restart, or exit your browser.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  } else if (yes == true || no == true) {
                      $("<p>You've already answered this question.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
                      check();
                  }
              }
          };
          start();
      });
  });