From 819ca3fa16e66b41dfecc54793fc972882f6c5ef Mon Sep 17 00:00:00 2001 From: endie Date: Mon, 29 Mar 2021 01:39:47 +0000 Subject: [PATCH] Improve code (thanks Alex for help!) --- main.js | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/main.js b/main.js index 14bf9b6..cf298c1 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,4 @@ -var request = new XMLHttpRequest() +let request = new XMLHttpRequest() request.open( "GET", "https://cors-proxy.blaseball-reference.com/database/allTeams", @@ -6,34 +6,23 @@ request.open( request.send(); request.onload = function() { - var teams = JSON.parse(this.response) - var totals = teams.map(team => team.lineup.length + team.rotation.length) + let teams = JSON.parse(this.response) + let totals = teams.map(team => team.lineup.length + team.rotation.length) // Get all player counts that there are from greatest to least - var playercounts = totals.slice().sort(function (x, y) { - return y - x - }).filter(function (item, pos, array) { - return !pos || item != array[pos - 1] - }) - - // Now make headers & lists for all the player counts - playercounts.forEach(function(item) { - document.body.insertAdjacentHTML("beforeend", - "

" - + item - + " players

") + // & make headers & lists for them + totals.slice().sort((x, y) => y - x) + .filter((item, pos, array) => !pos || item != array[pos - 1]) + .forEach(function(item) { + document.body.insertAdjacentHTML("beforeend", + `

${item} player${(item > 1 ? "s" : "")}

+ `) }) // Put the teams in the lists teams.forEach(function(item, pos) { document.getElementById("tlist" + totals[pos]).insertAdjacentHTML("beforeend", - "
  • " - + item.fullName - + " (" - + item.lineup.length - + " hitters, " - + item.rotation.length - + " pitchers)
  • ") + `
  • ${item.fullName} + (${item.lineup.length} hitter${(item.lineup.length > 1 ? "s" : "")}, + ${item.rotation.length} pitcher${(item.rotation.length > 1 ? "s" : "")})
  • `) }) document.getElementById("progress").remove()