I'm new in js please help, the code only return the calculation for the first input in the database. so it prints out the same value for every iteration.
it seems like the loop doesn't break, so it only calculates the first input. I've tried to understand it and kept changing the code but the result is either "NaN" or the first calculation.
const firebaseRef = firebase.database().ref('listing/);
firebaseRef.on("value", function(snapshot){
snapshot.forEach(snap=>{
const listing = snap.val();
document.getElementById("listing").innerHTML += `
<p class=input value=${dB.dBname}></p>
<p class=result></p>
`
})
$(document).each(function(){
$('.result').html(getAge($('.input').val())+" years");
});
// getAge code from https://stackoverflow.com/questions/10008050/get-age-from-birthdate/10008120
function getAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="listing"></div>