简体   繁体   中英

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

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/

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 .

You're also checking your if statements with a single equal sign = , try using either == or === when comparing.

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:

    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.

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 . You need to fix the errors before the script can run correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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