Order of assignment

As I go through the JS cirriculum I keep running into this and am wondering if the issue is part of the testing if FCC or actually a coding issue. The order of assignment makes the test not pass and I feel like it really shouldn’t matter for the code, here is an example where I have highlighted in ** ** the code I’m curious about:

switch (key) {
case “ArrowLeft”:

**keys.leftKey.pressed = isPressed;**

** // isPressed = keys.leftKey.pressed**

if(xVelocity === 0) {

  **player.velocity.x = xVelocity;**

** // xVelocity = player.velocity.x**
}

}

I have commented out the way I was doing it initially (the same operation just in reverse order). Please let me know if you have any options. thanks!

why do you think these two lines are the same?

I think they are the same because both sides of the = are the same values for each line just reversed in the order of operations, right?

assignment is not commutative
if you do

let a = 3;
let b = 4;
a = b;

what values do a and b have?

let a = 3;
let b = 4;
b = a;

and here?

ok, so in the first one a is 3 and b is 4, then a becomes 4
in the second one a is 3 and b is 4 then b becomes 3. So the order of operations is definitely important.

so in my example the thing needs to happen before it can be assigned? Like the isPressed varaible can’t be pre assigned to somthing that may happen, that thing has to happen then I can assign it.

Is that logic sound, I’m still struggling to connect the a,b and example from my code. Since we declare variables all the time then assign them a value with =

Thank you for taking the time to walk me through this.

can you give the link to the step? I can try to explain it better looking at the whole code of the project

I havent shared a link before on here does this work?

in this case the function movePlayer is not called yet, but it looks like you need to update the player object properties or the keys object properties based on the function parameters. So you need to check the input values to movePlayer, and use that to update other values

Right so it goes to the next step where those have been assigned, the question I had was just why that order matters, here is the link after successfully completed:

and this is where i intially put:

isPressed = keys.leftKey.pressed
xVelocity = player.velocity.x

And didn’t understand why isPressed and xVelocity had to be on the right side of the = for the code to pass

because the variables that are the function parameters exist only inside the function, updating them doesn’t affect anything.
The function parameters have the values that are needed to update the outside world, so you need to assign them to something or those values get lost.

Ok thank you for taking the time to respond

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.