954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Decrementation Not Working

Please see the comment on code below to see the problem.

<html>
	<head>
		<title>Sequence: x ^ n -1</title>
	</head>
	<body>
		<script type = "text/javascript">
		var sqn;
		var pow=1;
		var pans=1;
		do {
			sqn = prompt("Enter Sequence: ");
		}while(sqn<0);
		var psqn;
		psqn=parseFloat(sqn);
		if (psqn==0){
			document.write("0");
		}
		else if (psqn>0){
			document.write("9<font color=red> --- </font>");
			for (x=1;x<psqn;x++){
				pans=1;
				for (y=0;y<=pow;y++){
					pans = pans*10;
				}
				--pans;//this line does not work when user input is greater than 15
				pow++;
				document.write(pans + "<font color=red> --- </font>");
			}
		}
		</script>
	</body>
</html>
aladar04
Light Poster
38 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

line 21 - You put pans at 1 every time the loop is repeated.

~G

Graphix
Posting Pro in Training
432 posts since Aug 2009
Reputation Points: 82
Solved Threads: 74
 

Thanks for the effort of posting your VERY helpful answer but I guess that doesn't solve the issue. I intentionally assigned 'pans' to hold a value of '1' every time the loop is repeated to make sure that it will hold a correct value every time a new loop begins.

Additional info: The user inputted value (sqn) determines how many times the equation x^n-1 will be repeated. Example, if user input is 1 then:

10^1 - 1, the program will print "9".

If user input is 3, then:

10^1 - 1, the program loops, 10^2 -1, the program loops for the last time, 10^3-1.
The printed output would be:

9 --- 99 --- 999 ---

the sequence works if user input is !< 0 && <=15. It begins to fail when user input is >=16.

aladar04
Light Poster
38 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

That is an expected result. On most 32-bit platforms the largest javascript integer is 2^31-1 and the maximum precision of a floating point number is 15 digits.

fxm
Posting Pro
596 posts since Apr 2010
Reputation Points: 40
Solved Threads: 74
 

I see... Thanks...

aladar04
Light Poster
38 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: