0

Here's the snippet for my mini-app that calculates the sum of powers. For example, when you choose a = 4, b = 2 it gives 20 because 4^1 + 4^2 = 20.

function calculateHandler () {
    const resultOutput = document.querySelector("div");
    let helpResult = 0;
    let finalResult = 0;
    const a = parseInt(window.prompt("Choose the base"));
    const b = parseInt(window.prompt("Choose the exponent:"));
    for (let i = 1; i <= b; i++) {
        helpResult = Math.pow(a, i);
        finalResult = finalResult + helpResult;
        resultOutput.innerText = resultOutput.innerText + (a + "^" + i + " = " + helpResult + " current sum => " + finalResult + "\n")
    }};

I want to display every line of the calculation inside of div, but I want them to display every 0.5 second. Therefore I need to set timer, but I have no clues how to do it inside of the loop which is inside of the function. I tried an option, but it only waited before certain timer before executing every iteration at once. Any suggestions on how to solve this? :)

Abgar
  • 33
  • 4
  • so what you want is to show ```a``` then wait for 0.5 seconds then show ```b``` then wait for 0.5 seconds then show ```helpResult``` wait 0.5 seconds the ```finalResult``` then done? – seriously Jun 10 '21 at 22:50

0 Answers0