Closed rodrigomorales1 closed 1 year ago
It looks like the issue here is a case of variable scoping in javascript. This is a common gotcha in JS when using the var
keyword, and is one of the reasons ES6 replaces var
with let
and const
. It's confusing, but the var writer = ...
in the for-loop is not actually creating a new variable, since var
is function-scoped, so you're always getting the writer for the last character in the loop in the callback for writer
rather than the writer you think you're getting. I think if you change var
to const
or let
it should fix the issue, since those are block-scoped, and a for-loop is a block. TLDR; never use var
in javascript, always use let
and const
.
@chanind Using const
or let
solved the problem. I'm not familiar with the differences between let
, var
and const
. I'll read on that.
Thanks for helping out even though my issue is not related with the project but with Javascript itself!
Table of contents
The context
Consider the following minimal simple web application with screenshot below (I think it is long but I tried to write it as simple as possible, this is the shortest I could write it). It takes a word (stored in the
word
variable) and create a quiz for each character in the word. Whenever a mistake occurs, a counter is incremented.The problem
As you can see, in the definition of
onMistake
, I'm trying to cancel the quiz whenever the user makes a mistake in that character. The problem is that regardless on which character a mistake occurs, the quiz for the last character is cancelled. I think this happens because in the last iteration of the for loop that creates the quiz for each character, the last value forwriter
is that of the last character, so whenever Javascript tries to accesswriter
, it is accessing to the last quiz which is not what I want. I want each quiz to be able to reference itself in order to cancel the quiz.The question
I think this is a clear example of the XY problem.
The X problem here is: Cancel a quiz when a mistake occurs The Y-solution is: Reference the writer from within the onMistake method.