# War with robots: declension of nouns after numerals

In the first part of the Terminator, Rees Kyle talks about how cool the robots have learned to disguise themselves as humans. Now they have real skin and hair, they sweat, etc. He said about previous models that it was easy to distinguish them by rubber skin.

I think that even earlier models differed quite simply - they said: â€śSoldier Chris Katarn, killed 10 enemies, spent 342 cartridges, got 0 wounds,â€ť and so on, than immediately fired.

Indeed, until now, despite the development of the web, on many sites you can find "50 users", "1 comments", "0 messages", etc. But how pleasant is it when the site speaks to you in human language and correctly conjugates words by numbers.
')
And itâ€™s not at all easy to do. Below are simple ready-made functions that allow you to solve this problem in PHP and Javascript. They are so simple that it is not difficult to transfer them to any other language.

The function passes the number of entities for which you want to pick up endings, and an array of words (or endings for words) for numbers 1, 4, and 5. For example, ['oyster', 'oyster', 'oyster'].

#### Php

1. / **
2. * The function returns the ending for the plural of a word based on the number and array of endings.
3. * param \$ number Integer Number based on which you want to form the ending
4. * param \$ endingsArray Array Array of words or endings for numbers (1, 4, 5),
5. * for example array ('apple', 'apples', 'apples')
6. * return String
7. * /
8. function getNumEnding ( \$ number , \$ endingArray )
9. {
10. \$ number = \$ number % 100 ;
11. if ( \$ number > = 11 && \$ number <= 19 ) {
12. \$ ending = \$ endingArray [ 2 ] ;
13. }
14. else {
15. \$ i = \$ number % 10 ;
16. switch ( \$ i )
17. {
18. case ( 1 ) : \$ ending = \$ endingArray [ 0 ] ; break ;
19. case ( 2 ) :
20. case ( 3 ) :
21. case ( 4 ) : \$ ending = \$ endingArray [ 1 ] ; break ;
22. default : \$ ending = \$ endingArray [ 2 ] ;
23. }
24. }
25. return \$ ending ;
26. }

#### Javascript

1. / **
2. * The function returns the ending for the plural of a word based on the number and array of endings.
3. * param iNumber Integer Number based on which you want to form the ending
4. * param aEndings Array Array of words or endings for numbers (1, 4, 5),
5. * for example ['apple', 'apples', 'apples']
6. * return String
7. * /
8. function getNumEnding ( iNumber , aEndings )
9. {
10. var sEnding , i ;
11. iNumber = iNumber % 100 ;
12. if ( iNumber > = 11 && iNumber <= 19 ) {
13. sEnding = aEndings [ 2 ] ;
14. }
15. else {
16. i = iNumber % 10 ;
17. switch ( i )
18. {
19. case ( 1 ) : sEnding = aEndings [ 0 ] ; break ;
20. case ( 2 ) :
21. case ( 3 ) :
22. case ( 4 ) : sEnding = aEndings [ 1 ] ; break ;
23. default : sEnding = aEndings [ 2 ] ;
24. }
25. }
26. return sEnding ;
27. }

Do not forget to separately handle the case, for the number 0. Just write, for example, â€ś0 recordsâ€ť is not enough. It is necessary at least to write "There are no records" or to change the design, hiding an empty block with records at all.

UPD: Thanks to IGlukhov for correcting the illiterate title!

Source: https://habr.com/ru/post/105428/

All Articles