Skip to content

Instantly share code, notes, and snippets.

@GeKiStolyarov
Last active July 9, 2018 03:30
Show Gist options
  • Save GeKiStolyarov/8345d7e32ec3f5f2f27a3d3d8ab0573d to your computer and use it in GitHub Desktop.
Save GeKiStolyarov/8345d7e32ec3f5f2f27a3d3d8ab0573d to your computer and use it in GitHub Desktop.
Склонение числительных в javascript
function declOfNum(number, titles) {
const cases = [2, 0, 1, 1, 1, 2];
return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
}
// example
declOfNum(1, ['день', 'дня', 'дней']); // 1 день
declOfNum(2, ['день', 'дня', 'дней']); // 2 дня
declOfNum(5, ['день', 'дня', 'дней']); // 5 дней
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment