I started to work on my memory mini game, but stopped right after start :( For loop is not working for me.
My code:
const game = {
...
shuffledCards: [],
startGame: () => {
...
// clear variables
this.shuffledCards = [];
for (let i = 0; i < this.cardsCount; i++) {
this.shuffledCards.push(Math.floor(i/2));
}
}
}
I want to generate an array which looks like this[0, 0, 1, 1, 2, 2...], but that for loop returns an empty array. Do you know why? When I try to change variables from this to normal ones and paste the code into browser, it works...
-
3Possible duplicate of What does "this" refer to in arrow functions in ES6?Sebastian Simon– Sebastian Simon2018年02月18日 16:18:24 +00:00Commented Feb 18, 2018 at 16:18
1 Answer 1
Arrow functions do not inherit this. You need to rewrite your code as
const game = {
...
startGame() {
...
this....
}
...
}
answered Feb 18, 2018 at 16:21
Stefan Becker
6,0629 gold badges24 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js