U
U
Uniq2018-02-19 15:20:05
JavaScript
Uniq, 2018-02-19 15:20:05

How to draw the conclusion of the ending (a, s)?

I can't figure out how to display correctly:
111 Views
23 Views
1 View
decided to distribute on the condition of odd numbers, but it turned out not what I wanted.

let local =    {"views":[" просмотр"," просмотра"," просмотров"]}
let Views = (e)=>{
  if(e == 1) return e+local.views[0];
  if (((e >10) ? e[e.length-1] : e) % 2 == 0) {
    return e+local.views[1];
  }
  return e+local.views[2];
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2018-02-19
@padonnak

const indexes = [2, 0, 1, 1, 1, 2];

    /**
     *
     * @param {number} amount
     * @param {string[]} cases For slavic languages: ['яблоко', 'яблока', 'яблок']
     * @returns {string}
     */
    function pluralize(amount, cases) {
        const mod100 = amount % 100;
        const mod10 = amount % 10;
        const index = (mod100 > 4 && mod100 < 20) ? 2 : indexes[(mod10 < 5) ? mod10 : 5];
        return cases[index];
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question