Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Last active May 24, 2024 17:15
Show Gist options
  • Save alekstar79/7d57262bf96987d8b325f8ff0d75aca0 to your computer and use it in GitHub Desktop.
Save alekstar79/7d57262bf96987d8b325f8ff0d75aca0 to your computer and use it in GitHub Desktop.
Numeric declination
/**
* @example declination(5, ['комментарий','комментария','комментариев'])
* @param {Number} n The number for which the ending will be calculated
* @param {Array} titles Word variants for 1,2,5
* @returns {String}
*/
export function declination(n, titles)
{
return titles[(n % 10 === 1 && n % 100 !== 11) ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment