I'm trying to take the value of an input and keep it in an array. I want to detect when the user text 0, and I want to sum all the values in the array before the 0.
Here is my code:
<form id="demo">
<input type="number" id="lala">
<button onclick='funsuma(this.form.lala.value)' > NO <button/>
</form>
<script>
var global = [];
var suma = 0;
function funsuma(valor) {
global.push(valor);
for (i = 0; i < global.length; i++) {
if (global[i] == 0) {
suma += parseInt(global[i], 10);
alert("Es 0, tu suma es: " + suma);
return false;
} else {
//alert(global[i]);
document.getElementById("demo").innerHTML = "<input type='number' id='lala'> <button value='Envia' onclick='funsuma(this.form.lala.value)'> NO <button/>";
return false;
}
}
//console.log("global");
}
</script>
Aucun commentaire:
Enregistrer un commentaire