The essence of the task consists in accepting the age value via the prompt method, and then outputting the line with the necessary case for this very number. The code contains operand%, which, in theory, should read the remainder of the division by 10 and output the necessary information. In addition, I used the break construct to prevent the browser from displaying the message "you are the wrong age" years old, and then said that an error occurred. But, unfortunately, I added something superfluous and now the browser does not display anything.

let age; age = prompt('Введите число, не превышающее 120...'); while (age !== "" && age > 0 && typeof (age) == "number") { if (age % 10 === 0) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'лет' + '</b>'); } if (age % 2 === 0 && age / 3 === 0) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'года' + '</b>'); } if (age % 2 === 1) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'год' + '</b>'); } else { if (age < 0) { document.write("Возраст не может быть меньше нуля!"); break; } if (typeof(age) !== "number") { document.write('<b>' + 'Это не число!' + '</b>'); break; } } break; } 

  • 'age' is a string. And age is a variable with the value entered. - u_mulder
  • Still does not work, the error is somewhere else - Frey
  • Yes, there are solid errors everywhere ... - Qwertiy
  • I understand this, that's why I ask for help - Frey
  • one
    The task is elementary, the problem is elementary - a pencil-notebook-debugger in hand and forward. - Enikeyschik

2 answers 2

https://jsfiddle.net/2uht05jp/8/

 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) } 

  • Again this parseInt :( - Qwertiy

It does not work, since you have typeof (age) == "number" in the condition, and you enter a string ... And, accordingly, the if (typeof(age) !== "number") { condition also works

 let age; age = prompt('Введите число, не превышающее 120...'); while (age !== "" && age > 0 && typeof (age) == "string") { if (age % 10 === 0) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'лет' + '</b>'); } if (age % 2 === 0 && age / 3 === 0) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'года' + '</b>'); } if (age % 2 === 1) { document.write('<b>' + 'Вам ' + '</b>' + '<b>' + 'год' + '</b>'); } else { if (age < 0) { document.write("Возраст не может быть меньше нуля!"); break; } if (typeof(age) !== "number") { document.write('<b>' + 'Это не число!' + '</b>'); break; } } break; } 

If you need a number, return a number using parseInt()

  • Ah, that was how it was ... Thank you very much) - Frey