|
526 | 526 |
|
527 | 527 | function findSimilarGdpMedalCountry(country) { |
528 | 528 | const base = getCountryIndicators(country); |
529 | | - if (!base.gdp && !base.population) return null; |
| 529 | + |
| 530 | + const hasBaseGDP = Number.isFinite(base.gdp) && base.gdp > 0; |
| 531 | + const hasBasePopulation = Number.isFinite(base.population) && base.population > 0; |
| 532 | + |
| 533 | + if (!hasBaseGDP && !hasBasePopulation) return null; |
530 | 534 |
|
531 | 535 | const countries = getAvailableCountries(state.season, state.year); |
532 | 536 |
|
|
540 | 544 |
|
541 | 545 | const indicators = getCountryIndicators(candidate); |
542 | 546 |
|
| 547 | + const hasCandidateGDP = |
| 548 | + Number.isFinite(indicators.gdp) && indicators.gdp > 0; |
| 549 | + |
| 550 | + const hasCandidatePopulation = |
| 551 | + Number.isFinite(indicators.population) && indicators.population > 0; |
| 552 | + |
| 553 | + // skip medal-winning countries that cannot be used for the fair formula |
| 554 | + if (!hasCandidateGDP || !hasCandidatePopulation) return null; |
| 555 | + |
543 | 556 | let distance = 0; |
544 | 557 | let terms = 0; |
545 | 558 |
|
546 | | - if (base.gdp && indicators.gdp) { |
| 559 | + if (hasBaseGDP) { |
547 | 560 | distance += Math.abs(Math.log(indicators.gdp) - Math.log(base.gdp)); |
548 | 561 | terms++; |
549 | 562 | } |
550 | | - if (base.population && indicators.population) { |
| 563 | + |
| 564 | + if (hasBasePopulation) { |
551 | 565 | distance += Math.abs(Math.log(indicators.population) - Math.log(base.population)); |
552 | 566 | terms++; |
553 | 567 | } |
554 | 568 |
|
555 | | - if (terms === 0) return null; |
556 | | - |
557 | | - return { country: candidate, distance: distance / terms }; |
| 569 | + return { |
| 570 | + country: candidate, |
| 571 | + distance: distance / terms |
| 572 | + }; |
558 | 573 | }) |
559 | 574 | .filter(Boolean) |
560 | 575 | .sort((a, b) => d3.ascending(a.distance, b.distance)); |
|
0 commit comments