Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How can I make a function that distributes the #aporte with the number of people but not using input type number, instead using input type text?

I need help so I can distribute the #aporte (total) in the amount of people entered in the button (I can´t use an input type number, I need to use the input type text and when you enter a name and click on the button, it will be added to the distribution). I really don´t know how to do it, here´s the code:

HTML:

<h1>Expenses app</h1>
<p>Enter how much each person spent</p>

<p>Nombre</p>
<input type="text" id="nombre">
<br>
<p>Monto</p>
<input type="number" id="monto">
<br>
<button onclick="ir()">Enviar</button>
<br>
<p>Total: <span id="final"></span> </p>
<div id="total">
</div>
<p>A cada uno le toca aportar: <span id="aporte"></span></p>

script:

  function ir() {
    
    const nombre = document.getElementById("nombre").value;
 
    let monto = document.getElementById("monto").value;
  
    const total = document.getElementById("total");
    const final = document.getElementById("final");
    const aporte = document.getElementById("aporte");
  
    const lineBreak = document.createElement("br");
    let newTotal = document.createTextNode(` ${nombre} : ${monto} `);
    total.appendChild(lineBreak);
    total.appendChild(newTotal);
  
    monto = Number(monto) + Number(final.innerHTML);
  
    final.innerHTML = `${monto}`;
    aporte.innerHTML = `${monto}`;
  };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try it :

const arr = []
const nombre = document.getElementById("nombre")
const monto = document.getElementById("monto")
const total = document.getElementById("total")
const final = document.getElementById("final")
const aporte = document.getElementById("aporte")
const list = document.getElementById("list")
const average = document.getElementById("average")
function ir() {
    let nombreVal = nombre.value
    let montoVal = monto.value
    if(nombreVal === '' || montoVal === '') return
    let exist = arr.find(({name, score}) => name === nombreVal)
    if (exist) {
      exist.score = Number(montoVal)
    } else {
      arr.push({name:nombreVal, score: montoVal})
    }
    let totalScore = arr.reduce((total, ele) => total += Number(ele.score), 0)
    let totalPeople = arr.length
    list.textContent  = '';
    arr.forEach(ele => {
      let nodeNombre = document.createElement('p')
      nodeNombre.innerHTML = ele.name + ': ' + ele.score
      list.appendChild(nodeNombre);
    })
  
    final.innerHTML = `${totalScore}`;
    aporte.innerHTML = `${totalPeople}`;
    average.innerHTML =  `average : ${totalScore/totalPeople}`
  };
<h1>Expenses app</h1>
<p>Enter how much each person spent</p>

<p>Nombre</p>
<input type="text" id="nombre">
<br>
<p>Monto</p>
<input type="number" id="monto">
<br>
<button onclick="ir()">Enviar</button>
<br>
<p>Total: <span id="final"></span> </p>
<div id="list"></div>
<div id="total"></div>
<div id="total-people"></div>
<p id="average"></p>
<p>A cada uno le toca aportar: <span id="aporte"></span></p>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda