Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Why is the placement of the first declaration of my let variable being misinterpreted?

yumedoll

Active Coder
I have declared a few variables for a game I'm making about baking a cake and the variables will act as indicators of the amount of each ingredient to decide the outcome of the cake.

JavaScript:
 let mixamnt = flouramnt = sugaramnt = chocoamnt = milkamnt = eggamnt = butteramnt = redamnt = vanillaamnt = 0;

But when running the site, I noticed that a function involving mixamnt wasn't firing. When I checked using the console by trying to log it, it returns mixamnt is not defined despite it being declared as 0 at the very beginning of the js file. Doing the same with all the other "amnt" variables returns the same issue.

It was only after checking my code editor, I noticed the error "You might be leaking a variable (flouramnt) here", I realized the whole thing was misinterpreting a mention of the variable within either one of these functions as the declaration of the variable for some weird reason.

JavaScript:
  ondrop: function (event) {
    var draggableElement = event.relatedTarget;
    // do something when the draggable element is dropped into the dropzone
    var cls = draggableElement.classList;
    
  switch(true){
    
    case cls.contains("whisk"):
       draggableElement.style.zIndex = "30";
       mixing.pause();
    break;
    
    case cls.contains("flour"):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(whitepowder, 150, 10);
       flouramnt++;
    break;
    
    case cls.contains('cocoa'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(chocopowder, 60, 10);
       chocoamnt++;
    break;
    
    case cls.contains('sugar'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(whitepowder, 250, 20);
       sugaramnt++;
    break;
    
    case cls.contains('milk'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(whitepowder, 80, 40);
       milkamnt++;
    break;
    
    case cls.contains('egg'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(egg, 100, 100);
       eggamnt++;
    break;
    
    case cls.contains('butter'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(butter, 300, 100);
       butteramnt++;
    break;
    
    case cls.contains('reddrop'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(dropred, 400, 100);
       redamnt++;
    break;
    
    case cls.contains('vanilla'):
       draggableElement.style.transform = 'translate(0px, 0px)';
       draggableElement.style.webkitTransform = 'translate(0px, 0px)';
       ctx.drawImage(vanilla, 100, 100);
       vanillaamnt++;
   }
  }
JavaScript:
if(flouramnt > 1 && milkamnt > 1){
   ingredientbasis = true;
  
   if(mixamnt > 5 && chocoamnt == 0 && redamnt == 0){
     batter.style.backgroundImage = "url('cakebake/mixbatter/batterwhite.png')";
   }
  
 }

I've tried everything I could to fix this to no avail, syntax of the entire file seems fine to me. And attempting to place console.log() at the beginning of the file itself after the declarations works as expected. Giving me a nice clean "0" in the console on site load, but even then, directly typing the same variable name in the console returns the same error, despite that big juicy "0" that is supposed to be that very variable is right there.

Can anyone help me figure out what's going on? The website in question is here and the js file is here.
 
What happens if you declare all of the variables separately?
JavaScript:
let mixamnt = 0;
let flouramnt = 0;
let sugaramnt = 0;
let chocoamnt = 0;
let milkamnt = 0;
let eggamnt = 0;
let butteramnt = 0;
let redamnt = 0;
let vanillaamnt = 0;
 
This looks like it may be one of those tricky aspects of JavaScript...
JavaScript:
let a = b = c = 0;
is executed right to left, ie it's equivalent to
JavaScript:
c = 0;
b = c;
let a = b;
So b and c are created with global scope whereas a has block scope.

I can't see all of your code to understand the block structure, but it looks like you are trying to access your variables outside the block where your multiple assignment is, which means that the first one, and only the first one, is inaccessible.
Tha's also consistent with it not working when you split up the statement... now all the variables are limited to block scope.
 
This looks like it may be one of those tricky aspects of JavaScript...
JavaScript:
let a = b = c = 0;
is executed right to left, ie it's equivalent to
JavaScript:
c = 0;
b = c;
let a = b;
So b and c are created with global scope whereas a has block scope.

I can't see all of your code to understand the block structure, but it looks like you are trying to access your variables outside the block where your multiple assignment is, which means that the first one, and only the first one, is inaccessible.
Tha's also consistent with it not working when you split up the statement... now all the variables are limited to block scope.
That's what I thought at first, but that can't be the case, because @yumedoll said it still didn't work after declaring the variables separately.
 
This looks like it may be one of those tricky aspects of JavaScript...
JavaScript:
let a = b = c = 0;
is executed right to left, ie it's equivalent to
JavaScript:
c = 0;
b = c;
let a = b;
So b and c are created with global scope whereas a has block scope.

I can't see all of your code to understand the block structure, but it looks like you are trying to access your variables outside the block where your multiple assignment is, which means that the first one, and only the first one, is inaccessible.
Tha's also consistent with it not working when you split up the statement... now all the variables are limited to block scope.
The entire code is here, it's hosted this way
 
I still think it’s a scope problem. Declaring the variables separately with let won’t fix it because they still have block scope.
Your problem references are in callback functions that will be called asynchronously sometime later. This means that they do not share the execution context of the place where they are declared. Making your variables global should fix the problem if I’m right.
 
Last edited:
I still think it’s a scope problem. Declaring the variables separately with let won’t fix it because they still have block scope.
Your problem references are in callback functions that will be called asynchronously sometime later. This means that they do not share the execution context of the place where they are declared. Making your variables global should fix the problem if I’m right.
I actually tried that, I declared it outside of the window.onload but it didn't work either. So I moved it back in.
 
It’s an old trick. To be fair you shouldn’t use it nowadays. Compiling with strict will reject it now. Sorry. (Although you were using it implicitly with your multiple déclaré/assign)
You could try using the global window object, ie. window.mixamnt
 
It’s an old trick. To be fair you shouldn’t use it nowadays. Compiling with strict will reject it now. Sorry. (Although you were using it implicitly with your multiple déclaré/assign)
You could try using the global window object, ie. window.mixamnt
Hmmmmm.... Can you give me an example? I'm still not completely sure how I should use it
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom