Improve code (thanks Alex for help!)

This commit is contained in:
endie 2021-03-29 01:39:47 +00:00
parent 3932f996dc
commit 819ca3fa16
1 changed files with 13 additions and 24 deletions

37
main.js
View File

@ -1,4 +1,4 @@
var request = new XMLHttpRequest() let request = new XMLHttpRequest()
request.open( request.open(
"GET", "GET",
"https://cors-proxy.blaseball-reference.com/database/allTeams", "https://cors-proxy.blaseball-reference.com/database/allTeams",
@ -6,34 +6,23 @@ request.open(
request.send(); request.send();
request.onload = function() { request.onload = function() {
var teams = JSON.parse(this.response) let teams = JSON.parse(this.response)
var totals = teams.map(team => team.lineup.length + team.rotation.length) let totals = teams.map(team => team.lineup.length + team.rotation.length)
// Get all player counts that there are from greatest to least // Get all player counts that there are from greatest to least
var playercounts = totals.slice().sort(function (x, y) { // & make headers & lists for them
return y - x totals.slice().sort((x, y) => y - x)
}).filter(function (item, pos, array) { .filter((item, pos, array) => !pos || item != array[pos - 1])
return !pos || item != array[pos - 1] .forEach(function(item) {
}) document.body.insertAdjacentHTML("beforeend",
`<h1>${item} player${(item > 1 ? "s" : "")}</h1>
// Now make headers & lists for all the player counts <ul id="tlist${item}"></ul>`)
playercounts.forEach(function(item) {
document.body.insertAdjacentHTML("beforeend",
"<h1>"
+ item
+ " players</h1><ul id='tlist"
+ item
+ "'></ul>")
}) })
// Put the teams in the lists // Put the teams in the lists
teams.forEach(function(item, pos) { teams.forEach(function(item, pos) {
document.getElementById("tlist" + totals[pos]).insertAdjacentHTML("beforeend", document.getElementById("tlist" + totals[pos]).insertAdjacentHTML("beforeend",
"<li>" `<li>${item.fullName}
+ item.fullName (${item.lineup.length} hitter${(item.lineup.length > 1 ? "s" : "")},
+ " (" ${item.rotation.length} pitcher${(item.rotation.length > 1 ? "s" : "")})</li>`)
+ item.lineup.length
+ " hitters, "
+ item.rotation.length
+ " pitchers)</li>")
}) })
document.getElementById("progress").remove() document.getElementById("progress").remove()