0

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...

Martz89
  • 67
  • 2
  • 7
  • 3
    Possible duplicate of [What does "this" refer to in arrow functions in ES6?](https://stackoverflow.com/questions/28371982/what-does-this-refer-to-in-arrow-functions-in-es6) – Sebastian Simon Feb 18 '18 at 16:18

1 Answers1

3

Arrow functions do not inherit this. You need to rewrite your code as

const game = {
   ...
   startGame() {
     ...
     this....
   }
   ...
}
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30