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

278
Views
How Can I make JS recognize my number from input as one number?

If i type in my input for example 15 it is reading it as 1 and 5. How can I change it so javascript won't read it like two different values?

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

You need to convert the value to a number before checking for greater than or less than.

Without converting the strings to numbers, you are just sorting them in alphabetical order

For example:

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

Use Number(value) to convert the values to numbers.

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!