所有变量都具有全局范围,但函数仍然无法访问它们

All variables have global scope but functions still can't access them

我已经在我的代码中将所有变量设置为全局变量,但我的函数仍然无法访问它们,我相信这也会导致我的代码中的函数像 getValueOfEasy() 函数一样未定义(还有另一个原因为什么我的函数在定义时显示为“未定义”?)

感谢任何帮助,我的主要问题是我无法弄清楚为什么函数无法读取我的变量。

我的代码 link 在这里:http://jsfiddle.net/6s8y4p9k/6/

代码:

<html>
  



  <script>

function getValueOfEasy(){    
var easy = document.getElementById("easy").value;
}
function getValueOfMedium(){
var medium = document.getElementById("medium").value;
}
function getValueOfHard(){
var hard = document.getElementById("hard").value;
}


   

<!--gives number to sentence  -->
    var FirstE2;
    var FirstM2;
    var FirstH2;
    
    var SecondE2;
    var SecondM2;
    var SecondH2;
    
    var ThirdE2;
    var ThirdM2;
    var ThirdH2;
    
    
<!-- prints __ var sentce to schedule -->
   
    var FirstE;
    var FirstM;
    var FirstH;
  
    var SecondE;
    var SecondM;
    var SecondH;
    
    var ThirdE;
    var ThirdM;
    var ThirdH;
    
  


    
    function timeE() {
    
    if (easy < 4){
            FirstE = "do your # easy assignemnts first";
        SecondE2 = 3;
    }else if (easy === 3) {
        SecondE = "do your # easy assignments second";
        SecondE2 = 3;
      } else if (easy <= 2) {
        ThirdE = "Do your # easy assignemnts last ";
        ThirdE2 = 3;
      }

    }



    function timeM() {
      if (medium >= 3) {
        FirstM = "do your # medium assignments first";
        FirstM2 = 2;
      } else if (medium === 2) {
        SecondM = "do your # medium assignments second"
        SecondM2 = 2;
      } else if (medium === 1) {
        ThirdM = "Do your # medium assignments last"
        ThirdM2 = 2;
      }
    }
    
    
    
    function timeH() {
      if (hard >= 1) {
        FirstH = "do your # hard assignments first";
        FirstH2 = 1;
      } else {
        return;
   
    }
        
      
      var excitedE;
      var excitedM;
      var excitedH;


    function yesE() {
      excitedE = 3;
    }

    function mehE() {
      excitedE = 2;
    }

    function noE() {
      excitedE = 1;
    }

    function yesM {

      excitedM = 3;
    }

    function mehM() {
      excitedM = 2;
    }

    function noM() {
      excitedM = 1;
    }

    function yesH {
      excitedH = 3;
    }

    function mehH() {
      excitedH = 2;
    }

    function noH() {
      excitedH = 1;
    }
    
    
    
    
    
    
    


    function ExcitedE() {


      if (excitedE = 3 && FirstE2 = 3) {
        FirstE = "do your # easy assignments last";

      } else if (excitedE = 3 && FirstE2 = 3 && easy >= 4) {
        ThirdE = "do your # easy  assignments third";
      }
      //easy: 1st of list & yes excited
      
      
      else if (excitedE = 2 && FirstE2 = 3) {
        
        SecondE = "do your # easy assignments Second";

      } 
      //easy: 1st of list & maybe excited
      
      
      else if (excitedE = 1 && FirstE2 = 3) {
        FirstE = "do your # easy assignments first";

      } 
      //easy: 1st of list & not excited


     else if (excitedE = 3 && SecondE2 = 3) {
        SecondE = "do your # easy assignments second";
      }
      //easy: 2nd of list & yes excited
      
      
      else if (excitedE = 2 && SecondE2 = 3) {
        SecondE = "do your # easy assignments second";
      }

      //easy: 2nd of list and maybe excited
      
      
      else if (excitedE = 1 && SecondE2 = 3) {
        FirstE = "do your # easy assignments first";
      }
      //easy: 2nd and no


      if (excitedE = 3 && ThirdE2 = 3) {
        ThirdE = "do your # easy assignments third";
      }

      //easy: 3rd of list & yes excited
      
      
      else if (excitedE = 3 && ThirdE2 = 3) {
        SecondE = "do your # easy assignments second";
      }

      //easy: 3nd of list and maybe excited
      
      
      else if (excitedE = 2 && ThirdE2 = 3) {
        FirstE = "do your # easy assignments first";
      }
      //easy: 3rd and no
      else if (excitedE = 1 && ThirdE2 = 3) {
        FirstE = "do your # easy assignments first";
      }


    }





    
    

    function ExcitedM() {

      if (excitedM = 3 && FirstM2 = 3 && medium >= 2) {
        ThirdM = "do your # easy  assignments third";
      } else if (excitedM = 3 && FirstE2 = 3) {
        FirstM = "do your # easy assignments last";
      }
      //medium: 1st of list & yes excited


      if (excitedM = 3 && SecondM2 = 3) {
        SecondM = "do your # easy assignments second";
      }
      //medium: 2nd of list & yes excited
      else if (excitedM = 2 && SecondM2 = 3) {
        SecondM = "do your # easy assignments second";
      }

      //medium: 2nd of list and maybe excited
      else if (excitedM = 1 && SecondM2 = 3) {
        FirstM = "do your # easy assignments first";
      }
      //medium: 2nd and no

      if (excitedM = 3 && ThirdM2 = 3) {
        ThirdM = "do your # easy assignments third";
      }

      //medium: 3rd of list & yes excited
      else if (excitedM = 2 && ThirdM2 = 3) {
        SecondM = "do your # easy assignments second";
      }

      //medium: 3nd of list and maybe excited
      else if (excitedM = 3 && ThirdM2 = 3) {
        FirstM = "do your # easy assignments first";
      }
      //medium: 3rd and no

    } else {

      return;
    }


  


  


    function ExcitedH() {

      if (excitedE = 3 && FirstH2 = 3 && hard === 1) {
        ThirdH = "do your # hard  assignments last";
      } else if (excitedE = 3 && FirstH2 = 3) {
        FirstH = "do your # hard assignments first";
      }
      //hard: 1st of list & yes excited


      if (excitedE = 3 && SecondH2 = 3) {
        SecondH = "do your # hard assignments second";
      }
      //hard: 2nd of list & yes excited
      else if (excitedE = 2 && SecondH2 = 3) {
        SecondH = "do your # hard assignments second";
      }

      //hard: 2nd of list and maybe excited
      else if (excitedE = 1 && SecondH2 = 3) {
        FirstH = "do your # hard assignments first";
      }
      //hard: 2nd and no

      if (excitedE = 3 && ThirdH2 = 3) {
        ThirdH = "do your # hard assignments third";
      }

      //hard: 3rd of list & yes excited
      else if (excitedE = 2 && ThirdH2 = 3) {
        SecondH = "do your # hard assignments second";
      }

      //hard: 3nd of list and maybe excited
      else if (excitedE = 3 && ThirdH2 = 3) {
        FirstH = "do your # hard assignments first";
      }
      //hard: 3rd and no

    } else {

      return;
    }




    function showSchedule() {
      document.getElementById("HD1").style.display = "none";
      document.getElementById("homeworkSchedule").style.display = "block";
    }

    function showEnterBtn() {
      document.getElementById("save").style.display = "none";
      document.getElementById("enter").style.display = "block";
    }


  function showHard() {
      document.getElementById("MD1").style.display = "none";
      document.getElementById("HD1").style.display = "block";
    }



    function showMedium() {
      document.getElementById("ED1").style.display = "none";
      document.getElementById("MD1").style.display = "block";
    }


    <
    !--makes schedule-- >
    function makeHomeworkSchedule() {

      document.getElementById("first").innerHTML = FirstE + " " + FirstM + " " + FirstH;
      document.getElementById("second").innerHTML = SecondE + " " + SecondM + " " + SecondH;
      document.getElementById("third").innerHTML = ThirdE + " " + ThirdM + " "
      ThirdH;
    }

  </script>



  <body>


    <div id=ED1>


      <p>
        <center>
          Question 1, enter number?
        </center>
      </p>

      <center><input min="0" id="easy" type="number"/></center>

      <center>
        <p>This an other question, click one of the buttons?</p>

        <button class="button button3" onclick="yesE()">Yes</button>

        <button class="button button3" onclick="mehE()">Meh</button>

        <button class="button button3" onclick="noE()">No</button>
      </center>

      <h1> </h1>




      <center><button class="button button3" onclick="getValueOfEasy(); timeE(); ExcitedE(); showMedium(); " id="easyEnter">Enter</button></center>
    </div>


    




    <div id="MD1" style="display:none">



      <h1>
        Question 2?
      </h1>

      <input min="0" id="medium" type="text">

      <p>this is another question?</p>
      <button class="button button3" onclick="yesM()">Yes</button>
      <button class="button button3" onclick="mehM()">Meh</button>
      <button class="button button3" onclick="noM()">No</button>

      <h1> </h1>



      <button class="button button3" onclick="getValueOfMedium(); showHard(); timeM(); ExcitedM();" id="mediumEnter">Enter</button>
    </div>





    <div id="HD1" style="display:none">



      <p>
       Question 3?
      </p>



      <input min="0" id="hard" type="text">

      <p>WOah another question!?</p>
      <button class="button button3" onclick="yesH()">Yes</button>
      <button class="button button3" onclick="mehH()">Meh</button>
      <button class="button button3" onclick="noH()">No</button>

      <h1> </h1>




      <button id="save" class="button button3" onclick="getValueOfHard(); timeH(); ExcitedH(); makeHomeworkSchedule();">Save</button>

      
    </div>




    <div id="homeworkSchedule" style="display:none">
      <p>... paragprah ...</p>

      <h1>1st:</h1>
      <h5 id="first"></h5>
      <h1>2nd:</h1>
      <h5 id="second"></h5>
      <h1>3rd:</h1>
      <h5 id="third"></h5>

    </div>


  </body>

</html>
// declare your variable in global

var firstVariable;


function A(firstVariable){
  // do something
}

// call function
A(firstVariable)

// if u want to check is some variable equals some value use `===` instead of '='

// don't use `<!--- -->` in JS, use `//` or `/* -your comment- */`

您有几个语法错误需要首先修复。您在某些函数中缺少一些括号,您正在使用赋值运算符 = 作为比较器,并且缺少 timeE 的右括号。您需要先修复错误,脚本才能 运行 正确。

我首先看到错误的是你用 <!-- comment here --> 评论你的 javascript 代码,在 javascript 你用 /* comment here*/ 或 [=14= 评论].

您还使用单个等号 = 检查 if 语句,比较时尝试使用 =====

您的代码中有很多语法错误,您应该尝试使用带有语法检查器的代码编辑器。有几次您忘记关闭方括号或圆括号。

你的函数不是全局的,因为它们是在本地创建的(也就是在另一个函数内部),你可以在函数外部创建函数,然后在函数内部调用这些函数,如下所示:

    var excitedE;

    function yesE() {
      excitedE == 3;
    }

    function timeH() {
      yesE();
}

在 ES6+ 中,我们并没有真正使用 var 来创建变量,您应该使用 let 来创建会改变的变量,而 const 来创建永不改变的变量。

无论如何,我修复了下面代码段中的所有内容。

<html>
  <script>
    function getValueOfEasy() {
      let easy = document.getElementById("easy").value;
    }
    function getValueOfMedium() {
      let medium = document.getElementById("medium").value;
    }
    function getValueOfHard() {
      let hard = document.getElementById("hard").value;
    }

    /*  gives number to sentence */
    let FirstE2;
    let FirstM2;
    let FirstH2;

    let SecondE2;
    let SecondM2;
    let SecondH2;

    let ThirdE2;
    let ThirdM2;
    let ThirdH2;

    /* prints __ let sentence to schedule */

    let FirstE;
    let FirstM;
    let FirstH;

    let SecondE;
    let SecondM;
    let SecondH;

    let ThirdE;
    let ThirdM;
    let ThirdH;

    function timeE() {
      if (easy < 4) {
        FirstE = "do your # easy assignments first";
        SecondE2 = 3;
      } else if (easy === 3) {
        SecondE = "do your # easy assignments second";
        SecondE2 = 3;
      } else if (easy <= 2) {
        ThirdE = "Do your # easy assignments last ";
        ThirdE2 = 3;
      }
    }

    function timeM() {
      if (medium >= 3) {
        FirstM = "do your # medium assignments first";
        FirstM2 = 2;
      } else if (medium === 2) {
        SecondM = "do your # medium assignments second";
        SecondM2 = 2;
      } else if (medium === 1) {
        ThirdM = "Do your # medium assignments last";
        ThirdM2 = 2;
      }
    }

    /* Making this variables and functions global */
    let excitedE;
    let excitedM;
    let excitedH;

    function yesE() {
      excitedE == 3;
    }

    function mehE() {
      excitedE == 2;
    }

    function noE() {
      excitedE == 1;
    }

    function yesM() {
      excitedM == 3;
    }

    function mehM() {
      excitedM == 2;
    }

    function noM() {
      excitedM == 1;
    }

    function yesH() {
      excitedH == 3;
    }

    function mehH() {
      excitedH == 2;
    }

    function noH() {
      excitedH == 1;
    }

    function timeH() {
      if (hard >= 1) {
        FirstH = "do your # hard assignments first";
        FirstH2 = 1;
      } else {
        return;
      }
      /* Calling the functions here */
      yesE();
      mehE();
      noE();
      yesM();
      mehM();
      noM();
      yesH();
      mehH();
      noH();
    }

    function ExcitedE() {
      if (excitedE == 3 && FirstE2 == 3) {
        FirstE = "do your # easy assignments last";
      } else if (excitedE == 3 && FirstE2 == 3 && easy >= 4) {
        ThirdE = "do your # easy  assignments third";
      }
      //easy: 1st of list & yes excited
      else if (excitedE == 2 && FirstE2 == 3) {
        SecondE = "do your # easy assignments Second";
      }
      //easy: 1st of list & maybe excited
      else if (excitedE == 1 && FirstE2 == 3) {
        FirstE = "do your # easy assignments first";
      }
      //easy: 1st of list & not excited
      else if (excitedE == 3 && SecondE2 == 3) {
        SecondE = "do your # easy assignments second";
      }
      //easy: 2nd of list & yes excited
      else if (excitedE == 2 && SecondE2 == 3) {
        SecondE == "do your # easy assignments second";
      }

      //easy: 2nd of list and maybe excited
      else if (excitedE == 1 && SecondE2 == 3) {
        FirstE == "do your # easy assignments first";
      }
      //easy: 2nd and no

      if (excitedE == 3 && ThirdE2 == 3) {
        ThirdE == "do your # easy assignments third";
      }

      //easy: 3rd of list & yes excited
      else if (excitedE == 3 && ThirdE2 == 3) {
        SecondE == "do your # easy assignments second";
      }

      //easy: 3nd of list and maybe excited
      else if (excitedE == 2 && ThirdE2 == 3) {
        FirstE == "do your # easy assignments first";
      }
      //easy: 3rd and no
      else if (excitedE == 1 && ThirdE2 == 3) {
        FirstE == "do your # easy assignments first";
      }
    }

    function ExcitedM() {
      if (excitedM == 3 && FirstM2 == 3 && medium >= 2) {
        ThirdM == "do your # easy  assignments third";
      } else if (excitedM == 3 && FirstE2 == 3) {
        FirstM == "do your # easy assignments last";
      }
      //medium: 1st of list & yes excited

      if (excitedM == 3 && SecondM2 == 3) {
        SecondM == "do your # easy assignments second";
      }
      //medium: 2nd of list & yes excited
      else if (excitedM == 2 && SecondM2 == 3) {
        SecondM == "do your # easy assignments second";
      }

      //medium: 2nd of list and maybe excited
      else if (excitedM == 1 && SecondM2 == 3) {
        FirstM == "do your # easy assignments first";
      }
      //medium: 2nd and no

      if (excitedM == 3 && ThirdM2 == 3) {
        ThirdM == "do your # easy assignments third";
      }

      //medium: 3rd of list & yes excited
      else if (excitedM == 2 && ThirdM2 == 3) {
        SecondM == "do your # easy assignments second";
      }

      //medium: 3nd of list and maybe excited
      else if (excitedM == 3 && ThirdM2 == 3) {
        FirstM == "do your # easy assignments first";
      }
      //medium: 3rd and no
      else {
        return;
      }
    }

    function ExcitedH() {
      if (excitedE == 3 && FirstH2 == 3 && hard === 1) {
        ThirdH == "do your # hard  assignments last";
      } else if (excitedE == 3 && FirstH2 == 3) {
        FirstH == "do your # hard assignments first";
      }
      //hard: 1st of list & yes excited

      if (excitedE == 3 && SecondH2 == 3) {
        SecondH == "do your # hard assignments second";
      }
      //hard: 2nd of list & yes excited
      else if (excitedE == 2 && SecondH2 == 3) {
        SecondH == "do your # hard assignments second";
      }

      //hard: 2nd of list and maybe excited
      else if (excitedE == 1 && SecondH2 == 3) {
        FirstH == "do your # hard assignments first";
      }
      //hard: 2nd and no

      if (excitedE == 3 && ThirdH2 == 3) {
        ThirdH == "do your # hard assignments third";
      }

      //hard: 3rd of list & yes excited
      else if (excitedE == 2 && ThirdH2 == 3) {
        SecondH == "do your # hard assignments second";
      }

      //hard: 3nd of list and maybe excited
      else if (excitedE == 3 && ThirdH2 == 3) {
        FirstH == "do your # hard assignments first";
      } else {
        return;
      }

      //hard: 3rd and no
    }

    function showSchedule() {
      document.getElementById("HD1").style.display = "none";
      document.getElementById("homeworkSchedule").style.display = "block";
    }

    function showEnterBtn() {
      document.getElementById("save").style.display = "none";
      document.getElementById("enter").style.display = "block";
    }

    function showHard() {
      document.getElementById("MD1").style.display = "none";
      document.getElementById("HD1").style.display = "block";
    }

    function showMedium() {
      document.getElementById("ED1").style.display = "none";
      document.getElementById("MD1").style.display = "block";
    }

    /* makes schedule */
    function makeHomeworkSchedule() {
      document.getElementById("first").innerHTML =
        FirstE + " " + FirstM + " " + FirstH;
      document.getElementById("second").innerHTML =
        SecondE + " " + SecondM + " " + SecondH;
      document.getElementById("third").innerHTML = ThirdE + " " + ThirdM + " ";
      ThirdH;
    }
  </script>

  <body>
    <div id="ED1">
      <p>
        <center>
          Question 1, enter number?
        </center>
      </p>

      <center><input min="0" id="easy" type="number" /></center>

      <center>
        <p>This an other question, click one of the buttons?</p>

        <button class="button button3" onclick="yesE()">Yes</button>

        <button class="button button3" onclick="mehE()">Meh</button>

        <button class="button button3" onclick="noE()">No</button>
      </center>

      <h1></h1>

      <center>
        <button
          class="button button3"
          onclick="getValueOfEasy(); timeE(); ExcitedE(); showMedium(); "
          id="easyEnter"
        >
          Enter
        </button>
      </center>
    </div>

    <div id="MD1" style="display: none;">
      <h1>
        Question 2?
      </h1>

      <input min="0" id="medium" type="text" />

      <p>this is another question?</p>
      <button class="button button3" onclick="yesM()">Yes</button>
      <button class="button button3" onclick="mehM()">Meh</button>
      <button class="button button3" onclick="noM()">No</button>

      <h1></h1>

      <button
        class="button button3"
        onclick="getValueOfMedium(); showHard(); timeM(); ExcitedM();"
        id="mediumEnter"
      >
        Enter
      </button>
    </div>

    <div id="HD1" style="display: none;">
      <p>
        Question 3?
      </p>

      <input min="0" id="hard" type="text" />

      <p>WOah another question!?</p>
      <button class="button button3" onclick="yesH()">Yes</button>
      <button class="button button3" onclick="mehH()">Meh</button>
      <button class="button button3" onclick="noH()">No</button>

      <h1></h1>

      <button
        id="save"
        class="button button3"
        onclick="getValueOfHard(); timeH(); ExcitedH(); makeHomeworkSchedule();"
      >
        Save
      </button>
    </div>

    <div id="homeworkSchedule" style="display: none;">
      <p>... paragraphs ...</p>

      <h1>1st:</h1>
      <h5 id="first"></h5>
      <h1>2nd:</h1>
      <h5 id="second"></h5>
      <h1>3rd:</h1>
      <h5 id="third"></h5>
    </div>
  </body>
</html>