0

enter image description here

        <h1>Crystal Collector</h1>

        <div id="crystal1" class="crystals"><img src="assets/images/purplecrystal.jpg"></div>
        <div id="crystal2" class="crystals"><img src="assets/images/bluecrystal.jpg"></div>
        <div id="crystal3" class="crystals"><img src="assets/images/greencrystal.jpg"></div>
        <div id="crystal4" class="crystals"><img id="crystal4" src="assets/images/yellowcrystal.jpg"></div>


        <div id="targetNumber" class="data">Number to Guess:<span id="targetNum"></span> </div>
        <div id="results" class="data">Wins: <span id="wins"></span>    Losses:  <span id="losses"></span></div>
        <div id="userTotal" class="data">Your Total: <span id="total"></span></div>



        <script type="text/javascript">
        $(document).ready(function() {


            var targetNumber = Math.floor((Math.random()*138)+19);

            var crystal1 = Math.floor((Math.random()*12)+1);

            var crystal2 = Math.floor((Math.random()*12)+1);

            var crystal3 = Math.floor((Math.random()*12)+1);

            var crystal4 = Math.floor((Math.random()*12)+1);

            var userNum = 0;

            var wins = 0;

            var losses = 0;

            $("#wins").html(wins);

            $("#losses").html(losses);

            $("#total").html(userNum); 

            $("#targetNum").html(" " + targetNumber);

            function reset() {

                var targetNumber = Math.floor((Math.random()*138)+19);

                var crystal1 = Math.floor((Math.random()*12)+1);

                var crystal2 = Math.floor((Math.random()*12)+1);

                var crystal3 = Math.floor((Math.random()*12)+1);

                var crystal4 = Math.floor((Math.random()*12)+1);

                var userNum = 0;


                $("#total").html(userNum); 

            }

            function victory() {

                wins++;
                reset();
                $("#wins").html(wins);

            }

            function defeat() {

                losses++;
                reset();
                $("#losses").html(losses);

            }

            $("#crystal1").click(function(){
                userNum = userNum + crystal1;
                alert("Total User: " + userNum + "\n Crystal1: " + crystal1);
                $("#total").html(userNum);  

                if (userNum == targetNumber) {
                    userNum = 0;
                victory();

               }
               else if (userNum > targetNumber) {
                userNum = 0;
                defeat();
            }

            });

            $("#crystal2").click(function(){
                userNum = userNum + crystal2;
                alert("Total User: " + userNum + "\n Crystal2: " + crystal2);
                $("#total").html(userNum);  

                if (userNum == targetNumber) {
                    userNum = 0;
                victory();
               }
               else if (userNum > targetNumber) {
                userNum = 0;
                defeat();
               }                
            });

            $("#crystal3").click(function(){
                userNum = userNum + crystal3;
                alert("Total User: " + userNum + "\n Crystal3: " + crystal3);
                $("#total").html(userNum);  

                if (userNum == targetNumber) {
                    userNum = 0;
                victory();
               }
               else if (userNum > targetNumber) {
                userNum = 0;
                defeat();
               }
            });            

            $("#crystal4").click(function(){
                userNum = userNum + crystal4;
                alert("Total User: " + userNum + "\n Crystal4: " + crystal4);
                $("#total").html(userNum);  

                if (userNum == targetNumber) {

                victory();
                userNum = 0;
               }
               else if (userNum > targetNumber) {
                userNum = 0;
                defeat();
               }
            });

        });        
        </script>

    </body>

//My reset function does not seem to working. Each crystal should have a new value once there is a win or loss. I even had to write "userNum = 0" for each click function, even though I wrote that out in my reset function.

I do not understand what is wrong with my reset function.

Vamsi Krishna
  • 69
  • 1
  • 9
  • 1
    If you are asking why your `targetNumber` is not changing, it's because you put `var` in front of it in the `reset()` method. That made it a different variable than the higher scoped targetNumber, and it will not be updated. Actually you put var on all the variables in the method, so the same thing applies to them all. – Taplar Jun 21 '18 at 19:38
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Taplar Jun 21 '18 at 19:40

0 Answers0