0

I have a variable player.result which is created six times. Each of these times it is added to player.participant.taxGame.totincome.

Although it is not clear from the code (this is part of an app), this line works properly. So player.result is neatly added to player.participant.taxGame.totincome six times.

I however want to store each of those instances of player.result seperately in a variables which is called something like player.round1, player.round2 etc. I have tried to do this with a for loop.

It currently looks like this:

player.participant.taxGame.totincome = player.participant.taxGame.totincome + player.result;
for ( i=1; i < 6; i++) {
    player.rounds[i] = player.result
};

Where am I going wrong?

8
  • 1
    I'm not sure I understand the types of your variables and where your error is Commented Jun 6, 2019 at 5:22
  • Yes I understand. I am working in an app that someone else created which makes it slightly complicated. I should perhaps add that the first line works properly. Commented Jun 6, 2019 at 5:23
  • From what I see, you're setting 6 elements in an array (player.rounds) with the same value: player.result Commented Jun 6, 2019 at 5:24
  • 1
    Your description makes no sense. "I have a variable player.result which is created six times." No, player.result is not a variable, and variables are created once, not six times. Show code; don't paraphrase it. Commented Jun 6, 2019 at 5:25
  • @melpomene Thanks for your constructive criticism. I am just trying to explain it the best I can at the moment with my limited javascript experience. Commented Jun 6, 2019 at 5:32

1 Answer 1

1

Push your result to an array which represent the rounds.

player.participant.taxGame.totincome = player.participant.taxGame.totincome + player.result;

player.rounds.push(player.result);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your answer! Do I have to initialise player.rounds = [] somehwere?
Sorry, newbie here. But I got it to work. Thank you!

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.