JSFiddle - React, Tailwind, and code Playground
JavaScript
function num2str(n, text_forms) {
n = Math.abs(n) % 100; var n1 = n % 10;
if (n > 10 && n < 20) { return text_forms[2]; }
if (n1 > 1 && n1 < 5) { return text_forms[1]; }
if (n1 == 1) { return text_forms[0]; }
return text_forms[2];
}
try {
let age;
age = parseInt(prompt('Введите число, не превышающее 120...'))
if(age < 0 || age > 120 || isNaN(age)) {
document.write('Вы ввели неверное значение!')
} else {
document.write('Вам ' + age + ' '+num2str(age, ['год', 'года', 'лет']))
}
} catch(e) {
console.log(e)
}