Add array number and list it

1

What is the simplest way to do with array a kind of bank where I put a value in an array and show the same later. My problem is to modify the array , where the initial value would be 0 and hence as it is deposited the value will increase based on my logic there very simple.

var dinheiro = [];
	var totalSaque;
	
	function depositar() {
		

	
		var deposito = document.getElementById("txt").value;

	    totalSaque = dinheiro + deposito;
      
      document.getElementById("dep").innerHTML = "--valor depositado--";


		
	}



	function verConta() {
		
		document.getElementById("ex").innerHTML = totalSaque;
    document.getElementById("dep").innerHTML = "";
		
	}
<!DOCTYPE html>
<html>
<head>
	<title>banco</title>
</head>
<body>

<h1 style="float: left;">No Caixa:</h1><h1 id="ex" style="float: left;">0</h1>

</br></br></br></br></br>

<input id="txt" type="number">

<button onclick="depositar()">Depositar</button>
<button onclick="verConta()">ver Conta</button>


<p id="dep"></p>


</body>
</html>
    
asked by anonymous 01.02.2018 / 18:44

3 answers

2

Without the array it would look like this:

var totalSaque = 0;
	
	function depositar() {
	
		var deposito = document.getElementById("txt").value;

	    totalSaque += parseInt(deposito);
      
      document.getElementById("dep").innerHTML = "--valor depositado--";
		
	}

	function verConta() {
		
		document.getElementById("ex").innerHTML = totalSaque;
    document.getElementById("dep").innerHTML = "";
		
	}
<!DOCTYPE html>
<html>
<head>
	<title>banco</title>
</head>
<body>

<h1 style="float: left;">No Caixa:</h1><h1 id="ex" style="float: left;">0</h1>

<br><br><br><br><br>

<input id="txt" type="number">

<button onclick="depositar()">Depositar</button>
<button onclick="verConta()">ver Conta</button>


<p id="dep"></p>

</body></html>

With the array:

var dinheiro = [];
	
	function depositar() {
	
		var deposito = document.getElementById("txt").value;

	    dinheiro.push(parseInt(deposito));
      
      document.getElementById("dep").innerHTML = "--valor depositado--";
		

		
	}



	function verConta() {
  
   var soma = dinheiro.reduce(function(acumulador, valorAtual) { return acumulador + valorAtual;});
		
		document.getElementById("ex").innerHTML = soma;
    document.getElementById("dep").innerHTML = "";
		
	}
<!DOCTYPE html>
<html>
<head>
	<title>banco</title>
</head>
<body>

<h1 style="float: left;">No Caixa:</h1><h1 id="ex" style="float: left;">0</h1>

<br><br><br><br><br>

<input id="txt" type="number">

<button onclick="depositar()">Depositar</button>
<button onclick="verConta()">ver Conta</button>


<p id="dep"></p>

</body></html>
    
01.02.2018 / 19:22
2

To work with arrays you can start the variable as follows: totalSaque = new Array(); or totalSaque = [];

To store the values is also very simple, you use the push method or enter the index you want to change, create, modify or remove. Ex:

totalSaque.push("Novo-valor");

/* Ou */

totalSaque[1] = "Novo-valor";

Listing it is also not headache, you can do using the repeat structure

01.02.2018 / 19:11
2

The problem with the code is that it is not adding up where it should, adding to a variable that does not even have a value. For some reason I believe you are thinking that array is a type that keeps the sum, which is far from true. It's simpler than this:

var total = 0;
function depositar() {
    total += parseInt(document.getElementById("txt").value);
    document.getElementById("dep").innerHTML = "--valor depositado--";
}
function verConta() {
    document.getElementById("ex").innerHTML = total;
    document.getElementById("dep").innerHTML = "";
}
<!DOCTYPE html>
    <head>
        <title>banco</title>
    </head>
    <body>
        <h1 style="float: left;">No Caixa:</h1><h1 id="ex" style="float: left;">0</h1>
        </br></br></br></br></br>
        <input id="txt" type="number">
        <button onclick="depositar()">Depositar</button>
        <button onclick="verConta()">ver Conta</button>
        <p id="dep"></p>
    </body>
</html>

If you want to show everything:

var dinheiro = [];
var total = 0;
function depositar() {
    var valor = parseInt(document.getElementById("txt").value)
    total += valor;
    dinheiro.push(valor); //somente se for fazer algo com este valor depois
    document.getElementById("dep").innerHTML += valor + "<br>";
}
function verConta() {
    document.getElementById("ex").innerHTML = total;
    document.getElementById("dep").innerHTML = "";
}
<!DOCTYPE html>
    <head>
        <title>banco</title>
    </head>
    <body>
        <h1 style="float: left;">No Caixa:</h1><h1 id="ex" style="float: left;">0</h1>
        </br></br></br></br></br>
        <input id="txt" type="number">
        <button onclick="depositar()">Depositar</button>
        <button onclick="verConta()">ver Conta</button>
        <p id="dep"></p>
    </body>
</html>
    
01.02.2018 / 19:10