Last active
March 1, 2020 19:58
-
-
Save hiranya911/730b0c04e6dc501669eab954c4274069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.collection('cities').get().then(function(querySnapshot) { | |
querySnapshot.forEach(function(doc) { | |
const data = doc.data(); | |
display(data); | |
if (closeToMillion(data)) { | |
console.log(`${dat.name} is close to reaching 1 mil citizens.`); | |
} | |
}); | |
}); | |
/** | |
* Adds the given city to the HTML table. This function is type agnostic, and not | |
* affected by the exact type of fields. | |
*/ | |
function display(data) { | |
const table = document.getElementById('cities'); | |
const row = table.insertRow(0); | |
const cell1 = row.insertCell(0); | |
const cell2 = row.insertCell(1); | |
cell1.innerHTML = data.name; | |
cell2.innerHTML = data.population; | |
} | |
/** | |
* Checks if the current population is close to 1 million or more. Since the population | |
* field can contain string values, we perform an explicit type conversion before performing | |
* math operations. | |
*/ | |
function closeToMillion(data) { | |
const population = parseInt(data.population); | |
return population + 5000 >= 1000000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment