Skip to content

Instantly share code, notes, and snippets.

@yifanz
Created May 20, 2017 21:08
Show Gist options
  • Save yifanz/ffa36475028747abbf0c62f1a62d6028 to your computer and use it in GitHub Desktop.
Save yifanz/ffa36475028747abbf0c62f1a62d6028 to your computer and use it in GitHub Desktop.
US Population 2015
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<script>
$(document).ready(function() {
jQuery.support.cors = true;
var states = ["01", "02", "04", "05", "06", "08", "09", "10", "11", "12", "13", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "44", "45", "46", "47", "48", "49", "50", "51", "53", "54", "55", "56", "72"];
var api_key = '5e562d947f0fb77549cb73938cf1bec320ebe37e';
var url = "https://api.census.gov/data/2015/pep/population";
var results = [];
$.ajax({
type: 'GET',
url:url,
data: {
"get": "POP,GEONAME",
"key": api_key,
"for": "state:" + states.join(",")
},
success: function(r) {
//console.log(r);
var total = 0, max = 0, i, pop;
for (i = 1; i < r.length; i++) {
pop = parseInt(r[i][0]);
if (pop > max) max = pop;
total += pop;
}
$(".total").text(total);
$(".max").text(max);
}
});
});
</script>
<body>
<table>
<caption>
2015 US Population
</caption>
<tr>
<th>Total</th>
<th>Max</th>
</tr>
<tr>
<td class="total"></td>
<td class="max"></td>
</tr>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment