Home > Back-end >  ¿how can I make a function or anything that can divide the total of money by the total of names ente
¿how can I make a function or anything that can divide the total of money by the total of names ente

Time:06-06

I only need a function so that I can divide #total by the total of names that were entered on the button. If you know the answer please help me, i'm a newbie in Javascript.

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}`;
};
<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>

CodePudding user response:

Sorry I can't fully understand the meanings of your attributes' name, I guess 'nombre' is names of people and 'monto' means amount of all money. If so:

Maybe you need split method to count the amount of names, like:

const names = 'name1,name2,name3'

// divide above string by ','
const nameArray = names.split(',') // ['name1', 'name2', 'name3']

// get the amount of names
const count = nameArray.length // 3

then use arithmetic operators to get what you need.

If you want to show everyone's money, you can use forEach (different from map) to do something, like:

const nameArray = ['name1', 'name2', 'name3']
const money = { name1: 10, name2: 20, name3: 30 }

// you can use forEach
nameArray.forEach((name) => {
    // this function will run 3 times,
    // and the name attribute will be 'name1' 'name2' 'name3' respectively
    console.log(`${name}: ${money[name]}`)
    // you can do something here, for example, operate document
})

// or use map
const resultStringArray = nameArray.map((name) => {
    return `${name}: ${money[name]}`
})
// resultStringArray = ['name1: 10', 'name2: 20', 'name3: 30']

CodePudding user response:

i hope this fast written solution helps you.

var Persons = [];
window.onload = () => {

    var PersonHTMLElement = document.getElementById("person");
    var AmountHTMLElement = document.getElementById("amount");
    var TotalHTMLElement = document.getElementById("total");
    var PersonListHTMLElement = document.getElementById("final");
    var TotalDividedHTMLElement = document.getElementById("aporte");

    document.getElementById("calc").addEventListener("click", setPerson);

    function setPerson(){

        let person = PersonHTMLElement.value;
        let amount = AmountHTMLElement.value;

        Persons.push({
            Name:person,
            Amount:parseFloat(amount)
        });

        PersonHTMLElement.value = "";
        AmountHTMLElement.value = "";
    
        setTotal();
    }
    function setTotal(){

        TotalHTMLElement.innerHTML = "";

        let PersonsList = "";
        let Total = 0;
        for(let i = 0;i < Persons.length; i  ){
            Total  = Persons[i].Amount;
            PersonsList  = `${Persons[i].Name}: ${Persons[i].Amount} <br>`;
        }
        
        TotalHTMLElement.innerHTML = PersonsList;
        PersonListHTMLElement.innerHTML = Total;
        TotalDividedHTMLElement.innerHTML = Total / Persons.length;
    }
}
<html>
    <head>
        <script src="myscript.js"></script>
    </head>
    <body>
        <h1>Expenses app</h1>
        <p>Enter how much each person spent</p>

        <p>Nombre</p>
        <input type="text" id="person">
        <br>
        <p>Monto</p>
        <input type="number" id="amount">
        <br>
        <br>
        <button id="calc">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>
    </body>
</html>

  • Related