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

1

1 Answer 1

3

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

const game = {
   ...
   startGame() {
     ...
     this....
   }
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.