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

260
Views
which one is bigger?! a problem in my js code

I write a js code to determine which number is bigger but the result is not perfect! when I input two number with same digit numbers, the result is correct. For example when I input "2" and "3" the result is "3" but when I input "2" in first field and "55" in the second field, the result is "2".
Thank you in advance.
sorry for my weak English.

    
function biggerOne() {
    var x = document.getElementById("firstNumber").value;
    var y = document.getElementById("secondNumber").value;
  
    if (x>y){     document.getElementById("Result").innerHTML=x;
    
     } else {
    
    document.getElementById("Result").innerHTML=y;
        }
       
    }
<!DOCTYPE html>
<head>
    <script src="show bigger.js"></script>
</head>
<body>
    <p>insert first number</p>
    <input type="number" id="firstNumber" ر>
    <p> insert second number</p>
    <input type="number" id="secondNumber">
    <button onclick="biggerOne()"> result </button>
    <!---it is so important to insert value in the below code line-->
    <p id="Result" value=""></p>


</body>

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

change type of x and y to number and then try to compare.for convert you can use Number or parsint

if (Number(x) > Number(Y))
//some code
else
//some code
about 4 years ago · Juan Pablo Isaza Report

0

Document.getElementById() returns "String" type value and comparison between two string will act differently. Better convert those string types to integer.

var x = document.getElementById("firstNumber").value;
var y = document.getElementById("secondNumber").value;

if (parseInt(x) > parseInt(y))
{     
  document.getElementById("Result").innerHTML=x;
} 
else 
{
  document.getElementById("Result").innerHTML=y;
}
about 4 years ago · Juan Pablo Isaza Report

0

<!DOCTYPE html>

<head>
  <script src="show bigger.js"></script>
</head>

<body>
  <p>insert first number</p>
  <input type="number" id="firstNumber" ر>
  <p> insert second number</p>
  <input type="number" id="secondNumber">
  <button onclick="biggerOne()"> result </button>
  <!---it is so important to insert value in the below code line-->
  <p id="Result" value=""></p>
  <script>
    function biggerOne() {
      var x = document.getElementById("firstNumber").value;
      var y = document.getElementById("secondNumber").value;
      if (parseInt(x) > parseInt(y)) {
        document.getElementById("Result").innerHTML = x;

      } else {

        document.getElementById("Result").innerHTML = y;
      }

    }
  </script>

</body>

Use the parseInt method.

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!