Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Calculate value from an array and return total in another array

I'm creating a project in which when I click on the Add button, I add the value entered in the first input to an array. After entering as many values ​​as possible I would like to calculate the numbers in the array and enter them in an array containing the total and show it in the console.

I was able to get the input values ​​to add to the array but can't calculate them afterwards. Can anyone kindly tell me how can I do?

HTML

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="stile.css">
</head>
<body>

    <form>

        <div>
            <input type="number" id="importo">
        </div>

        <button type="submit" id="aggiungi">AGGIUNGI</button>

        <button type="submit" id="calcola">CALCOLA</button>
    </form>
    
    <script src="script.js"></script>
</body>

JS

 const aggiungi = document.getElementById("aggiungi")

const calcola = document.getElementById("calcola")

const importazione = document.getElementById("importo")




const spese = []


console.log(importazione, motivazione)


aggiungi.onclick = function(e) {

e.preventDefault()

spese.push(Number(importazione.value))

console.log(importazione.value)

console.log(spese)
      
}





/* calcola.onclick = function (e) {

    e.preventDefault()

    let somma = 0

    let conti = []

    for (let i=0; i = spese.length; i++) {
        
        conti = [somma += spese[i]]

        console.log(conti)
      
    }

}  */
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use array.reduce:

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

const aggiungi = document.getElementById("aggiungi")

const calcola = document.getElementById("calcola")

const importazione = document.getElementById("importo")

const spese = []


console.log(importazione);


aggiungi.addEventListener("click", e => {
  spese.push(Number(importazione.value))

  console.log(importazione.value)

  console.log(spese)

});

calcola.addEventListener("click", () => {
  const result = spese.reduce((previousValue, currentValue) => previousValue + currentValue, 0);

  console.log(result);
});
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="stile.css">
</head>

<body>

  <form>

    <div>
      <input type="number" id="importo">
    </div>

    <button type="button" id="aggiungi">AGGIUNGI</button>

    <button type="button" id="calcola">CALCOLA</button>
  </form>

  <script src="script.js"></script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!