Jump to content

If (x is an integer)


mortalc

Recommended Posts

you can mix and match, all these examples are doing the same thing as far as evaluating the prompts return, but I provided additional functionality in mine, , as your requirements specified that you wanted to keep asking the user over and over in the event they didn't provide you a number value.
So does the simple change I made. mortalc already has a working loop, the only trouble with it was that it allowed decimal values. The parseInt() function wasn't working quite as it was expected to. So that's why I suggested using Number() instead.
Link to comment
Share on other sites

  • Replies 76
  • Created
  • Last Reply
So does the simple change I made. mortalc already has a working loop, the only trouble with it was that it allowed decimal values. The parseInt() function wasn't working quite as it was expected to. So that's why I suggested using Number() instead.
oh yeah...an early morning bug on my part? :)
Link to comment
Share on other sites

Ok, that works. But the code as a whole doesn't work at all. I have absolutely no idea why. it doesn't even get to the first 'window.alert'.

window.alert("This is a program that searches for a solution to Fermat's Last Theorem. Please note that this program cannot deal with values above around 1.5 x 10^308. To keep this limit, the program will workout the maximum value of n for you.");var test = prompt ("This is just to make sure that you have Scripted windows enabled.");var A = NaN;while (isNaN(A) || !(A===Math.floor(A))){	var AA = prompt ("Type in the maximum value of a and b");	var A = Number(AA);	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}var N = log((Number.MAX_VALUE)/2)/log(A);window.alert ("n will be less than or equal to " +N);var x = 1;while !(x===0){	var X = prompt ("Type 0 to begin");	var x = Number(X);	if !(x===0)	{		window.alert (x + " is not zero. Please type in 0 to begin.");	}}var Q = 0;while (x <= 0) {	var a = Math.floor(Math.random()*A + 1);	var b = Math.floor(Math.random()*A + 1);	var n = Math.floor(Math.random()*(N - 2) + 3);	var c = Math.pow((Math.pow(a,n) + Math.pow(b,n)),(1/n))	if (c = Number.POSITIVE_INFINITY) {		window.alert ("Sorry, the numbers you entered were too big. please try again")		var x = 2;		var c = 1.1;	}	if (c === Math.floor(c)) {		window.alert ("Congratulations! You have disproved Fermat's Last Theorem.");		window.alert ("The values are coming up.");		window.alert ("Get a pen and paper ready.");		window.alert ("You will not see them again.");		window.alert ("Here it goes!");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("For a^n + b^n = c^n");		window.alert ("I'll repeat that, just for good measure.");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("Done.");		window.alert ("You've found solutions to Fermat's Last Theorem!");		var result = window.confirm("Do you want to search for more?");		if (!result) {			var x = 2;		}	}x--;Q++;document.write (+Q);}

Link to comment
Share on other sites

You have some syntax errors:

while !(x===0) {   ...	if !(x===0) {	  ...

The syntax for a conditional test should be in parens:

while (x!==0) {   ...   if (x!==0) {	  ...

or

while (!(x===0)) {   ...   if (!(x===0)) {	  ...

Using a debugging console like Firebug for FireFox will show these kinds of errors.Edit:I also noticed an error on this line:if (c = Number.POSITIVE_INFINITY)You should have a double '=' otherwise your assigning the value of Number.POSITIVE_INFINITY to the variable c

Link to comment
Share on other sites

here you go try this, after correcting var N = log((Number.MAX_VALUE)/2)/log(A); to var N = Math.log((Number.MAX_VALUE)/2)/Math.log(A);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><style type="text/css"></style></head><body><script type="text/javascript">/*<![CDATA[*//*---->*/window.alert("This is a program that searches for a solution to Fermat's Last Theorem. Please note that this program cannot deal with values above around 1.5 x 10^308. To keep this limit, the program will workout the maximum value of n for you.");var test = prompt ("This is just to make sure that you have Scripted windows enabled.");var A = NaN;while (isNaN(A) || !(A===Math.floor(A))){	var AA = prompt ("Type in the maximum value of a and b");	var A = Number(AA);	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}var N = Math.log((Number.MAX_VALUE)/2)/Math.log(A);window.alert ("n will be less than or equal to " +N);var x = 1;while (x!==0){	var X = prompt ("Type 0 to begin");	var x = Number(X);	if (x!==0)	{	window.alert (x + " is not zero. Please type in 0 to begin.");	}}var Q = 0;while (x <= 0) {	var a = Math.floor(Math.random()*A + 1);	var b = Math.floor(Math.random()*A + 1);	var n = Math.floor(Math.random()*(N - 2) + 3);	var c = Math.pow((Math.pow(a,n) + Math.pow(b,n)),(1/n))	if (c == Number.POSITIVE_INFINITY) {		window.alert ("Sorry, the numbers you entered were too big. please try again")		var x = 2;		var c = 1.1;	}	if (c === Math.floor(c)) {		window.alert ("Congratulations! You have disproved Fermat's Last Theorem.");		window.alert ("The values are coming up.");		window.alert ("Get a pen and paper ready.");		window.alert ("You will not see them again.");		window.alert ("Here it goes!");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("For a^n + b^n = c^n");		window.alert ("I'll repeat that, just for good measure.");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("Done.");		window.alert ("You've found solutions to Fermat's Last Theorem!");		var result = window.confirm("Do you want to search for more?");		if (!result) {			var x = 2;		}	}x--;Q++;document.write (+Q);}/*--*//*]]>*/</script> </body></html>

Link to comment
Share on other sites

OK, I've got it down to just one error on an online debugger. But that error is 'uncaught JavaScript runtime exception: ReferenceError: "window" is not defined.'How can "window" not be defined?

Link to comment
Share on other sites

guys! hello , just ran posted code, and it ran went through process with no errors, just check....yeah cloaking device is off
Yea me too. I ran exactly what you posted without any errors. I was just suggesting it without 'window' to see if mortalc could get it to work that way.
Link to comment
Share on other sites

The tryit editor is not an online debugger, in fact it's best not to use that to test code. Just fire it up with Firefox and Firebug enabled, press F12 to open Firebug and you'll see all error messages printed there. The window object will always be defined in a browser, only something like Windows Script Host would not have the window object defined.

Link to comment
Share on other sites

What browser are you using? As JSG said, you shouldn't use the try it editor on w3schools (even though I tried it and it did work without errors :) ). Try saving your code to test.html or something and open it with your browser.

Link to comment
Share on other sites

I did. But absolutely nothing comes up (just a blank page).I use Intenet Explorer (I don't have much choice in the matter... for now.)

Link to comment
Share on other sites

Well there won't be anything actually on the page since it's all javascript and your not using document.write to write anything either. All it does is show a succession of prompts and alerts.Sounds almost as if you have scripting disabled. Check the settings in IE to make sure scripting is enabled.

Link to comment
Share on other sites

What version of IE? On IE8 it's in Internet Options->Security tab->Custom Level button, then scroll down to the Scripting section.Edit:Only enable the Active Scripting. Leave everything else alone. You don't want to muck around with scripting settings too much because then you can open up security holes.

Link to comment
Share on other sites

just did google, and surprise, surprise its the prompt() which is causing the problem, its disabled for security reasons in IE, that why there was no problems using firefox which i was using to test, but when i use ie8, the prompt are bypassed, and it run the rest of the script without any input. good ole IE does it again.

Link to comment
Share on other sites

And Heres the fix from http://pipwerks.com/2007/08/08/a-cross-bro...ascript-prompt/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script><!--function iePrompt(str){	var settings = "dialogWidth: 290px; dialogHeight: 160px; center: yes; edge: raised; scroll: no; status: no;";	return window.showModalDialog("iePrompt.html", str, settings);}function cbPrompt(str){	try {		if(window.showModalDialog){ return iePrompt(str); }		else { return prompt(str, ""); }	} catch (e) { 		return false; 	}	 }--></script><style type="text/css"></style></head><body><script type="text/javascript">/*<![CDATA[*//*---->*/window.alert("This is a program that searches for a solution to Fermat's Last Theorem. Please note that this program cannot deal with values above around 1.5 x 10^308. To keep this limit, the program will workout the maximum value of n for you.");var test = cbPrompt('This is just to make sure that you have Scripted windows enabled.');//prompt ("This is just to make sure that you have Scripted windows enabled.");var A = NaN;while (isNaN(A) || !(A===Math.floor(A))){	var AA = cbPrompt('Type in the maximum value of a and b');//prompt ("Type in the maximum value of a and b");	var A = Number(AA);	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}var N = Math.log((Number.MAX_VALUE)/2)/Math.log(A);window.alert ("n will be less than or equal to " +N);var x = 1;while (x!==0){	var X = cbPrompt('Type 0 to begin');//prompt ();	var x = Number(X);	if (x!==0)	{	window.alert (x + " is not zero. Please type in 0 to begin.");	}}var Q = 0;while (x <= 0) {	var a = Math.floor(Math.random()*A + 1);	var b = Math.floor(Math.random()*A + 1);	var n = Math.floor(Math.random()*(N - 2) + 3);	var c = Math.pow((Math.pow(a,n) + Math.pow(b,n)),(1/n))	if (c == Number.POSITIVE_INFINITY) {		window.alert ("Sorry, the numbers you entered were too big. please try again")		var x = 2;		var c = 1.1;	}	if (c === Math.floor(c)) {		window.alert ("Congratulations! You have disproved Fermat's Last Theorem.");		window.alert ("The values are coming up.");		window.alert ("Get a pen and paper ready.");		window.alert ("You will not see them again.");		window.alert ("Here it goes!");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("For a^n + b^n = c^n");		window.alert ("I'll repeat that, just for good measure.");		window.alert ("a = "+a);		window.alert ("b = "+b);		window.alert ("n = "+n);		window.alert ("c = "+c);		window.alert ("Done.");		window.alert ("You've found solutions to Fermat's Last Theorem!");		var result = window.confirm("Do you want to search for more?");		if (!result) {			var x = 2;		}	}x--;Q++;document.write (+Q);}/*--*//*]]>*/</script> </body></html>

now create page called iePrompt.html which will create a form that looks similar to prompt() input box

<!DOCTYPE HTML><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><!--   Developed by Philip Hutchison, August 2007.   An explanation for this example (and more examples) can be found  at http://pipwerks.com/lab/--><title>Prompt</title><style type="text/css"><!--body {	background-color: #DFDEE6;	margin: 10px;	font-size: 9pt;	font-family: Tahoma;	text-align: center;}input {	width: 72px;}input#promptText {	display: block;	width: 250px;	margin: 1em auto; }label {	display: block;	width: 250px;	margin: 0px auto;	text-align: left;}#btnOK {	margin-right: 10px;}--></style><script type="text/javascript">function formSubmit(){	var str = document.getElementById("promptText").value;	if(str){  window.returnValue = str;  }	else {  window.returnValue = false;  }	window.close();}function formCancel(){	window.returnValue = false;	window.close();}window.onload = function (){	var str = window.dialogArguments;	if(str){		document.getElementsByTagName("label")[0].innerHTML = str;		document.getElementById("promptText").focus();	}}</script></head><body onbeforeunload="formSubmit();"><form id="promptForm" method="post" action="" onSubmit="formSubmit(); return false;" onReset="formCancel()"><label for="promptText"></label><input name="promptText" id="promptText" type="text"><input name="btnOK" id="btnOK" value="OK" type="submit"><input name="btnCancel" id="btnCancel" value="Cancel" type="reset"></form></body></html>

Link to comment
Share on other sites

Ok, it had scripts enabled, but not 'scriplets'. Also it had scripted windows diabled.Ed:It sill doesn't work. However, seeing as I'm on a school system, I can't do anything to change it. In a month or so I'll get my own laptop, and then we shall see.Until then, I'll have to take your word for it.Thank you all for your help!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...