I have a function which takes the numbers from an input field, and sums them. However, I want the equation to continue to add the sum together until the result is a single number. I feel like I need an if statement somewhere here, but Im not sure how to write it. Could I have some suggestions for how to amend this code?
function getSum() {
const input = document.getElementById('dateInput').value;
var sum = 0;
for (var i = 0; i < input.length; i++) {
const num = parseInt(input.charAt(i));
if (!isNaN(num)) {
sum += num;
}
}
document.getElementById("result").textContent = "Your life path number is: " + sum;
}
<div class="container">
<div class="cell-1"><input type="date" id="dateInput"></div>
<div class="cell-2"><h1>Blah Blah</h1></div>
<div class="cell-3"><input type="date" id="dateInput"></div>
<div class="cell-4" id="result"></div>
<div class="cell-5"><button onclick="getSum()">Calculate</button></div>
<div class="cell-6"></div>
<div class="cell-7"></div>
<div class="cell-8"></div>
<div class="cell-9"></div>
</div>
Aucun commentaire:
Enregistrer un commentaire