From 2657f0181ce8c544606cbc03bc18e14381cbef71 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 16 Sep 2020 19:49:46 +0100 Subject: [PATCH] removed repeating code --- public/js/misc.js | 16 +++++++++++++ public/js/script.js | 56 +++++++-------------------------------------- 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/public/js/misc.js b/public/js/misc.js index c8a7138a2..cf889b6be 100644 --- a/public/js/misc.js +++ b/public/js/misc.js @@ -382,3 +382,19 @@ function getASCII() { } return ret; } + +function getPositionString(number) { + let numend = "th"; + let t = number % 10; + let h = number % 100; + if (t == 1 && h != 11) { + numend = "st"; + } + if (t == 2 && h != 12) { + numend = "nd"; + } + if (t == 3 && h != 13) { + numend = "rd"; + } + return number + numend; +} \ No newline at end of file diff --git a/public/js/script.js b/public/js/script.js index 7e34bf488..19f815d8e 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -1988,35 +1988,11 @@ function showResult(difficultyFailed = false) { globalLbString = "global: not qualified"; } else if (e.data.globalLeaderboard.insertedAt >= 0) { if (e.data.globalLeaderboard.newBest) { - let pos = e.data.globalLeaderboard.insertedAt + 1; - let numend = "th"; - let t = pos % 10; - let h = pos % 100; - if (t == 1 && h != 11) { - numend = "st"; - } - if (t == 2 && h != 12) { - numend = "nd"; - } - if (t == 3 && h != 13) { - numend = "rd"; - } - globalLbString = `global: ${pos}${numend} place`; + let str = getPositionString(e.data.globalLeaderboard.insertedAt + 1); + globalLbString = `global: ${str} place`; } else { - let pos = e.data.globalLeaderboard.foundAt + 1; - let numend = "th"; - let t = pos % 10; - let h = pos % 100; - if (t == 1 && h != 11) { - numend = "st"; - } - if (t == 2 && h != 12) { - numend = "nd"; - } - if (t == 3 && h != 13) { - numend = "rd"; - } - globalLbString = `global: already ${pos}${numend}`; + let str = getPositionString(e.data.globalLeaderboard.foundAt + 1); + globalLbString = `global: already ${str}`; } } @@ -2028,27 +2004,11 @@ function showResult(difficultyFailed = false) { dailyLbString = "daily: not qualified"; } else if (e.data.dailyLeaderboard.insertedAt >= 0) { if (e.data.dailyLeaderboard.newBest) { - let pos = e.data.dailyLeaderboard.insertedAt + 1; - let numend = "th"; - if (pos === 1) { - numend = "st"; - } else if (pos === 2) { - numend = "nd"; - } else if (pos === 3) { - numend = "rd"; - } - dailyLbString = `daily: ${pos}${numend} place`; + let str = getPositionString(e.data.dailyLeaderboard.insertedAt + 1); + dailyLbString = `daily: ${str} place`; } else { - let pos = e.data.dailyLeaderboard.foundAt + 1; - let numend = "th"; - if (pos === 1) { - numend = "st"; - } else if (pos === 2) { - numend = "nd"; - } else if (pos === 3) { - numend = "rd"; - } - dailyLbString = `daily: already ${pos}${numend}`; + let str = getPositionString(e.data.dailyLeaderboard.foundAt + 1); + dailyLbString = `daily: already ${str}`; } }