The problem is that in JavaScript (unlike C or Java), just because the operands of the / operator are both integers, doesn't mean that the value returned will be an integer. Rather, if the result of the division has a fraction part, JavaScript returns the exact value as a real number. This can be fixed by using the Math.floor() method to keep 'n' an integer value:
Code:
<script language="javascript">
var n = null;
var r,rev=0;
n = parseInt(window.prompt("Enter number"));
while(n>0)
{
r = n % 10;
rev = rev *10 +r;
n = Math.floor (n/10);
}
alert(rev);
</script>
Bookmarks