Created
November 9, 2015 10:56
-
-
Save SergSlon/9f73f299d60403260f04 to your computer and use it in GitHub Desktop.
Склонение слов по числам на JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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