If you type in var n 121, and in var s 2 or the numbers 3, 4, 5, 6, 7, 8, 9, then document.write not executed, but, for example, enter in var s 1, then document.write will be executed . What is the reason, tell me?

 var n = prompt("Введите число", ""); var s = prompt("Угадайте число", ""); if (n > s) document.write("n больше s"); 
  • What kind of nonsense did you write? Can you properly associate at least two words? - AseN
  • Well, if you enter 121 in the variable n and in the variable s 2 then document.write ("n is greater than s"); will not be fulfilled!? - adidassler

2 answers 2

Use the parseInt function that converts the first argument to a number, that is:

 var n = prompt('Введите число', ''); var s = prompt('Угадайте число', ''); if (parseInt(n) > parseInt(s)) { document.write('n больше s'); } 

That should work.

  • 2
    better Number(x) - karmadro4 pm
  • 2
    Why is this? - Specter
  • karmadro4 is no better - Zowie
  • Everything works fine its mistake! var n = prompt ("Enter a number", ""); n = parseInt (n); var s = prompt ("Guess the number", ""); s = parseInt (s); if (n> s) {document.write ("greater than"); } - adidassler pm
  • @Spectre, if only because JS does not have Int or Floar types, but there is a generic Number type. In general, look in the documentation, the behavior of these functions is quite different. - karmadro4

 var n = +prompt("Введите число", ""); var s = +prompt("Угадайте число", ""); if (n > s) { document.write("n больше s"); }