0

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>
3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by aladar04
-1

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

~G

Edited by Graphix: n/a

Votes + Comments
sorry, not helpful.
0

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.

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.

Edited by fxm: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.