4 Replies - 32449 Views - Last Post: 23 December 2007 - 05:46 AM

#1 rpavithra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-November 07

reverse a number using javascript

Posted 26 November 2007 - 05:25 AM

got to reverse a no inputed by the user.well,the code that i tried is as follows:
<html>
<script language="javascript">
var a,b;
a=parseInt(prompt("enter a no."));
while(a>0)
{
b=a % 10;
a=parseInt(a/10);
}
document.write(b+a)
</script>
</html>
Is This A Good Question/Topic? 0
  • +

Replies To: reverse a number using javascript

#2 beef  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 141
  • Joined: 02-November 06

Re: reverse a number using javascript

Posted 26 November 2007 - 10:32 AM

View Postrpavithra, on 26 Nov, 2007 - 06:25 AM, said:

got to reverse a no inputed by the user.well,the code that i tried is as follows:
<html>
<script language="javascript">
var a,b;
a=parseInt(prompt("enter a no."));
while(a>0)
{
b=a % 10;
a=parseInt(a/10);
}
document.write(b+a)
</script>
</html>


It's overwriting b each time it loops through so all you're ending up with is the last digit. Try moving your document.write up inside your loop.
Was This Post Helpful? 0
  • +
  • -

#3 xenofied  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-July 06

Re: reverse a number using javascript

Posted 14 December 2007 - 05:47 PM

Hereunder is a solution for your perusal. Albeit the code as a bug, it should still serve as a quick reference to better help clarify how recursion works:

<html>
<script language = "javascript">
	//It's important to pre-assign b to a value
	//before using it in the loop. In your case
	//you'll need to set it to zero.

	var a, b = 0;
	a = parseInt(prompt("Enter number to reverse: "));

	//The idea is to continually divide 'a' (resulting quotients) by 10;
	//then multiply the remainders 'b' by 10, adding them to each 
	//other until there is no remainder left (i.e. b = zero).

	while(a > 0)
	{		
		b = b * 10
		b = b + parseInt(a % 10)
		a = parseInt(a / 10)
	}
	document.write("Reversed number: ", b)
</script>
</html>


BUG:When you precede a number with zero you get a wrong output from that which is desired. Anyway, i'll leave you with this piece and let you figure out what the problem is. Wherever you're stuck, feel free to ask questions. I am sure someone will definitely respond. Have fun B)

This post has been edited by xenofied: 14 December 2007 - 05:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: reverse a number using javascript

Posted 23 December 2007 - 03:12 AM

Moved to Javascript Forums.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_ch__*


Reputation:

Re: reverse a number using javascript

Posted 23 December 2007 - 05:46 AM

There goes the neighborhood ;)
String.prototype.reverse = function() {
	return this.split('').reverse().join('');
};
var n = 1234;
alert(('' + n).reverse()); // 4321


Was This Post Helpful? 1

Page 1 of 1