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.

Revisions

  1. SergSlon renamed this gist Nov 9, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. SergSlon created this gist Nov 9, 2015.
    29 changes: 29 additions & 0 deletions js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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 );// яблок