Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why does Java work while JavaScript does not? I tried using this in Java, and it worked just fine, but in JavaScript, it only prints "Not a palindrome." How come? I referred palindrome in javascript article for a better understanding Many thanks.

var n = 121;
 var sum = 0, r;
 var temp = n;    
 while(n>0)
   {    
    r = n % 10;   
    sum = (sum*10)+r;    
    n = n/10;    
   }    
  if(temp==sum)    
    console.log("It is a Palindrome number.");
  else  
    console.log("Not a palindrome");


What I have tried:

I tried using this in Java, and it worked just fine, but in JavaScript, it only prints "Not a palindrome." How come?
Posted
Updated 12-Sep-22 22:05pm

1 solution

Java and Javascript are completely different beasts.
Here
Quote:
n = n/10;
You need integer division, replace it with
JavaScript
n = Math.floor(n/10); 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900