Skip to content
Advertisement

How to use generate multiple of something with a single function using vanilla JS?

In the snippet below, I’m generating a single math problem on load, and if the New Problem button is clicked, the problem will refresh with a new one.

I want 3 problems to be able to display on the page. In pseudo code, this would be:

forEach class called .problem, display a different math problem.

This is what I have thus far, but I’m not getting the result I need.

JavaScript
JavaScript

Advertisement

Answer

First thing should be your selector. .problem is what you have in your class.

Second thing, move the random number calculation inside the loop to get different problem.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement