Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
¿Cómo puedo hacer que JS reconozca mi número de entrada como un número?

Si escribo mi entrada, por ejemplo, 15, lo está leyendo como 1 y 5. ¿Cómo puedo cambiarlo para que javascript no lo lea como dos valores diferentes?

 function generate() { var x = document.forms["form"]["firstInput"].value; var y = document.forms["form"]["secondInput"].value; if (isNaN(x) || isNaN(y)) { document.getElementById("errorf").innerHTML = "Input has to be a number!"; document.getElementById("wynikf").innerHTML = ""; } else if (x > y) { document.getElementById("wynikf").innerHTML = "First value can't be lower than second value"; } else if (x == null || x == "" || y == null || y == "") { document.getElementById("errorf").innerHTML = "Input can't be empty!"; document.getElementById("wynikf").innerHTML = ""; } else { for (var counter = x; counter <= y; counter++) { document.getElementById("result").innerHTML += counter + "\n"; } } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe convertir el valor en un número antes de comprobar si es mayor o menor que.

Sin convertir las cadenas en números, solo las está ordenando en orden alfabético

Por ejemplo:

 "15" > "3" // false because '1' comes before '3' 15 > 3 // true because the number 15 is greater than 3

Use Number(value) para convertir los valores a números.

 function generate() { var x = Number(document.forms["form"]["firstInput"].value); var y = Number(document.forms["form"]["secondInput"].value); if (isNaN(x) || isNaN(y)) { document.getElementById("errorf").innerHTML = "Input has to be a number!"; document.getElementById("wynikf").innerHTML = ""; } else if (x > y) { document.getElementById("wynikf").innerHTML = "First value can't be lower than second value"; } else { for (var counter = x; counter <= y; counter++) { document.getElementById("result").innerHTML += counter + "\n"; } } }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!