Created
December 11, 2018 14:53
-
-
Save denchistyakov/7e0fb67653aa6aafb403d81e22772e0d to your computer and use it in GitHub Desktop.
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
/** | |
* @param {number} count | |
* @param {string[]} forms | |
* @return {string} | |
*/ | |
function plural(count, forms) { | |
// В большинстве случаев возвращаем третью форму | |
let idx = 2; | |
let numbersInCount = count.toString().split(''); | |
let lastNumber = Number(numbersInCount[numbersInCount.length - 1]); | |
// 11 - 19 | |
if (numbersInCount.length > 1 && Number(numbersInCount[numbersInCount.length - 2]) === 1) { | |
idx = 2; | |
// 2, 3, 4 | |
} else if (lastNumber >= 2 && lastNumber < 5) { | |
idx = 1; | |
// 1 | |
} else if (lastNumber === 1) { | |
idx = 0; | |
} | |
return forms[idx]; | |
} | |
console.log( | |
plural(0, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(1, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьяна', | |
plural(2, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьяны', | |
plural(5, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(11, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(19, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(21, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьяна', | |
plural(22, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьяны', | |
plural(25, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(111, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
plural(119, ['обезьяна', 'обезьяны', 'обезьян']) === 'обезьян', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment