Skip to content

Instantly share code, notes, and snippets.

@SergSlon
Created November 9, 2015 10:56
Show Gist options
  • Select an option

  • Save SergSlon/9f73f299d60403260f04 to your computer and use it in GitHub Desktop.

Select an option

Save SergSlon/9f73f299d60403260f04 to your computer and use it in GitHub Desktop.
Склонение слов по числам на JS
var wordCase = function( num, words ) {
var word = '';
num = Math.abs( num );
if ( num.toString().indexOf( '.' ) > -1 ) {
word = words[ 2 ];
} else {
word = (
num % 10 === 1 && num % 100 !== 11
? words[ 1 ]
: num % 10 >= 2 && num % 10 <= 4 && ( num % 100 < 10 || num % 100 >= 20)
? words[ 2 ]
: words[ 0 ]
);
}
return word;
};
/* использование: */
var words = [ 'яблок', 'яблоко', 'яблока' ];// [ 'ноль яблок', 'одно яблоко', 'два яблока' ]
wordCase( 0, words );// яблок
wordCase( 1, words );// яблоко
wordCase( 2, words );// яблока
wordCase( 5, words );// яблок
wordCase( 10, words );// яблок
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment