简体   繁体   English

所有变量都有全局 scope 但函数仍然无法访问它们

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

I have made all of my variables global in my code but my function still cant access them and I believe it's also causing my functions in my code to be undefined like the getValueOfEasy() function (is there another reason why my functions are coming up as "undefined" when they are defined?)我已经在我的代码中将所有变量设为全局变量,但我的 function 仍然无法访问它们,我相信这也会导致我的代码中的函数像 getValueOfEasy() function 一样未定义(是否还有另一个原因导致我的函数出现为定义时“未定义”?)

I appreciate any help, my main problem is I can't figure out why my variables can't be read by the functions.感谢任何帮助,我的主要问题是我无法弄清楚为什么函数无法读取我的变量。

the link to my code is here: http://jsfiddle.net/6s8y4p9k/6/我的代码链接在这里: http://jsfiddle.net/6s8y4p9k/6/

code:代码:

<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>

First thing I see wrong is that you're commenting your javascript code with <!-- comment here --> , in javascript you comment with /* comment here*/ or with // comment here .我看到的第一件事是错误的,你用<!-- comment here -->评论你的 javascript 代码,在 javascript 你用/* comment here*/// comment here

You're also checking your if statements with a single equal sign = , try using either == or === when comparing.您还使用单个等号=检查 if 语句,尝试在比较时使用=====

There are a lot of syntax errors in your code, you should try using a code editor with a syntax checker.您的代码中有很多语法错误,您应该尝试使用带有语法检查器的代码编辑器。 There are a few occasions that you forgot to close brackets or parenthesis.在某些情况下,您会忘记关闭方括号或圆括号。

Your functions aren't global because they were created locally(aka inside another function), you can create the functions outside a function, and then just call these functions inside the function like here:你的函数不是全局的,因为它们是在本地创建的(也就是在另一个函数中),你可以在 function 之外创建函数,然后在 function 中调用这些函数,如下所示:

    var excitedE;

    function yesE() {
      excitedE == 3;
    }

    function timeH() {
      yesE();
}

In ES6+ we don't really use var to create a variable, you should be using let to variables that will change, and const for variables that never change.在 ES6+ 中,我们并没有真正使用var来创建变量,您应该对会改变的变量使用let ,对永不改变的变量使用const

Anyway, I fixed everything in the snippet below.无论如何,我修复了下面代码片段中的所有内容。

 <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>

// 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- */`

You have several syntax errors that you need to fix first.您有几个语法错误需要首先修复。 You are missing some parenthesis in some functions, you are using the assignment operator = as a comparator, and you are missing a closing bracket for timeE .您在某些函数中缺少一些括号,您正在使用赋值运算符=作为比较器,并且缺少timeE的右括号。 You need to fix the errors before the script can run correctly.在脚本可以正确运行之前,您需要修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM