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

asked Feb 18, 2018 at 16:16
1

1 Answer 1

3

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

const game = {
 ...
 startGame() {
 ...
 this....
 }
 ...
}
answered Feb 18, 2018 at 16:21
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.