← Back to challenges
← Weird Numbers·Calculate How Many Numbers with Unique Digits →

Who's That Polygon?

JavaScriptHardstringsdata_structures

Instructions

Every polygon has an official name. Individual polygons are named according to the number of sides, combining a Greek-derived numerical prefix with the suffix -gon, e.g. pentagon, dodecagon.

Write a function that takes an integer n as argument and returns the official name of a n-sided polygon.

The following prefixes table is used to construct the name of a polygon.

OnesTensHundreds
1hena-, hen-10deca-100hecta-
2di-, do-20icosi-, icosa-200dohecta-
3tri-, tria-30triaconta-300triahecta-
4tetra-40tetraconta-400tetrahecta-
5penta-50pentaconta-500pentahecta-
6hexa-60hexaconta-600hexahecta-
7hepta-70heptaconta-700heptahecta-
8octa-80octaconta-800octahecta-
9ennea-90enneaconta-900enneahecta-

For polygons with 3 through 9 sides, simply add "-gon" to the ones prefixes. For 10 through 99 sides, you return the tens prefix followed by the ones prefix and then by "-gon". For 100 through 999 sides, you start from the hundreds prefix, then add the tens prefix and finally the ones prefix followed by "-gon".

For polygons with more than 999 sides, the following prefixes are used similarly to how "conta-" and "hecta-" are used for tens and hundreds places respectively:

PlacePrefix
10conta-
100hecta-
1000chilia-
10,000myria-
100,000decamyria-
200,000icosamyria-
300,000triacontamyria-
1,000,000mega-
1,000,000,000giga-
1,000,000,000,000tera-

And finally, the 10¹⁰⁰-gon is called googolgon and the ∞-gon (with a countable infinite number of sides) is called apeirogon.

Examples

polygonName(3) ➞ "triangle"
// exception of: tri-gon

polygonName(4) ➞ "quadrilateral"
// exception of: tetra-gon

polygonName(5) ➞ "pentagon"
// penta-gon

polygonName(7) ➞ "heptagon"
// hepta-gon

polygonName(19) ➞ "enneadecagon"
// ennea-deca-gon

polygonName(36) ➞ "triacontahexagon"
// triaconta-hexa-gon

polygonName(428) ➞ "tetrahectaicosioctagon"
// tetrahecta-icosi-octa-gon

polygonName(12345) ➞ "myriadichiliatriahectatetracontapentagon"
// myria-dichilia-triahecta-tetraconta-penta-gon

Notes

  • The triangle, quadrilateral and nonagon are exceptions, although the regular forms trigon, tetragon, and enneagon are sometimes encountered as well.
  • Check Tests to spot other exceptions.
javascript
Loading editor…
⌘ ↵ to run
Walks through the solution with reasoning and edge cases.