• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to keep pushing new values from a dataset on click event to an array

Resolved: How to keep pushing new values from a dataset on click event to an array

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Trying to add the dice roll value from a data set every time the roll dice click is triggered and a new number is produced from a random math function.
Currently, the value is added to the array but only one value is added and the array is not incremented with each new roll value.
What is the best approach to push each new value to the array? Including a maximum of 25 additions.
The goal is to first console log each dice roll value and then eventually display them in the html as a list of 25 roll results.
Code Below:


Answer:

You should put let res1Arr = [] outside the function, since it’s now returning to empty array on every function call. Also, to avoid double results in array on every function call, you should place array.push outside the forEach loop, since it’s iterating twice for each call.

If you have better answer, please add a comment about this, thank you!

arrays foreach function javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How applied a multiple textures images or one image into a cube in qt 6 with qml?

22/03/2023

Resolved: More than one condition in CASE clause after renaming of column in SQL

22/03/2023

Resolved: Filter objects with random number keys in Es6

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.