From: Steinar H. Gunderson Date: Wed, 26 Jul 2023 22:23:29 +0000 (+0200) Subject: Remove some repetition. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=ad5df1fb3de10ee30e9442d8d0ac72becf208e0a;hp=cfabd4fbb72ecc39666f06ffe4aa5af3d3ec4446;p=pkanalytics Remove some repetition. --- diff --git a/ultimate.js b/ultimate.js index 292534f..27a79b9 100644 --- a/ultimate.js +++ b/ultimate.js @@ -750,59 +750,24 @@ function get_chosen_category() { function write_main_menu(chosen_category) { let elems = []; - if (chosen_category === 'general') { - let span = document.createElement('span'); - span.innerText = 'General'; - elems.push(span); - } else { - let a = document.createElement('a'); - a.appendChild(document.createTextNode('General')); - a.setAttribute('href', '#general'); - elems.push(a); - } - - if (chosen_category === 'offense') { - let span = document.createElement('span'); - span.innerText = 'Offense'; - elems.push(span); - } else { - let a = document.createElement('a'); - a.appendChild(document.createTextNode('Offense')); - a.setAttribute('href', '#offense'); - elems.push(a); - } - - if (chosen_category === 'defense') { - let span = document.createElement('span'); - span.innerText = 'Defense'; - elems.push(span); - } else { - let a = document.createElement('a'); - a.appendChild(document.createTextNode('Defense')); - a.setAttribute('href', '#defense'); - elems.push(a); - } - - if (chosen_category === 'playing_time') { - let span = document.createElement('span'); - span.innerText = 'Playing time'; - elems.push(span); - } else { - let a = document.createElement('a'); - a.appendChild(document.createTextNode('Playing time')); - a.setAttribute('href', '#playing_time'); - elems.push(a); - } - - if (chosen_category === 'per_point') { - let span = document.createElement('span'); - span.innerText = 'Per point'; - elems.push(span); - } else { - let a = document.createElement('a'); - a.appendChild(document.createTextNode('Per point')); - a.setAttribute('href', '#per_point'); - elems.push(a); + const categories = [ + ['general', 'General'], + ['offense', 'Offense'], + ['defense', 'Defense'], + ['playing_time', 'Playing time'], + ['per_point', 'Per point'] + ]; + for (const [id, title] of categories) { + if (chosen_category === id) { + let span = document.createElement('span'); + span.innerText = title; + elems.push(span); + } else { + let a = document.createElement('a'); + a.appendChild(document.createTextNode(title)); + a.setAttribute('href', '#' + id); + elems.push(a); + } } document.getElementById('mainmenu').replaceChildren(...elems);