Categories
Mastering Development

if a positive number is equal to its reverse

I’m making a program to receive a positive number and output "yes" if it’s equal to its reverse;
otherwise, output "no".

What I’ve done so far:

HTML


    <div class="column1">
    <div class="input">
      <button onclick="problem()"> Run the program </button>
    </div>
    <strong><p id="output"> </p></strong>
  </div>

JS


function problem() {

  var outputObj = document.getElementById("output");

  var a = parseInt(prompt("Please enter a number: ", ""));
 
  
outputObj.innerHTML = "number: " + a + "<br><br>";

var reverse = 0;

while (a > 0){
  num = a % 10;  // the last digit
  reverse = (reverse *10) + num; // calculating the reverse
  a = Math.floor(a / 10); // go to next digit
}

if ( reverse == a){
  outputObj.innerHTML = "yes";
}

else {
outputObj.innerHTML = "no";
}

  outputObj.innerHTML =  outputObj.innerHTML + "<br><br>" + "program ended";
  document.getElementsByTagName("button")[0].setAttribute("disabled","true");
}

Leave a Reply

Your email address will not be published. Required fields are marked *