diff --git a/main.ts b/main.ts index 530291a..c0394bc 100644 --- a/main.ts +++ b/main.ts @@ -124,16 +124,17 @@ function renderPokemonIndex(pokemons: Array): string { ${ability.description} `; }) - .join('\n'); - const getStatBar = (value: number, maxValue: number) => { - const percentage = Math.round((value / maxValue) * 100); - const greenThreshold = 35; - const yellowThreshold = 20; + .join('\n'); + const getStatBar = (value: number, sumValues: number, maxValue: number) => { + const absolute_percentage = Math.round((value / maxValue) * 100); + const relative_percentage = Math.round((value / sumValues) * 100); + const greenThreshold = 100 / (6 - 1); // 6: because there are 6 stats + const yellowThreshold = 100 / (6 + 1); // +-1: to move a bit more/less from 6 let backgroundColor; - if (percentage >= greenThreshold) { + if (relative_percentage >= greenThreshold) { backgroundColor = 'limegreen'; - } else if (percentage >= yellowThreshold) { + } else if (relative_percentage >= yellowThreshold) { backgroundColor = 'gold'; } else { backgroundColor = 'tomato'; @@ -142,20 +143,22 @@ function renderPokemonIndex(pokemons: Array): string { return `
-
+
`; }; const statsTableRows = pokemon.stats - .map(stat => ` + .map(stat => { + const sum_stats = pokemon.stats.reduce((a, b) => a.value + b.value); + return ` ${(stat.name === 'hp') ? 'HP': stat.name.charAt(0).toUpperCase() + stat.name.slice(1)}: ${stat.value} - ${getStatBar(stat.value, 255)} - `) - .join('\n'); + ${getStatBar(stat.value, sum_stats, 255)} + `; + }).join('\n'); return `