Jump to content

If (x is an integer)


mortalc

Recommended Posts

  • Replies 76
  • Created
  • Last Reply

Ok. So, would this work:

var AA = prompt ("Type a value for A");var A = parseInt(AA, 10);if (isNaN(A)){  alert(A + ' is not a number.');}

Link to comment
Share on other sites

Ok. All these questions have been for a program that searches for a solution to Fermat's last Theorem (FTTDK: that a^n + b^n does not equal c^n where a,b, c and n are all positive integers and n is greater than 2). For some reason, it isn't working. Here it is:

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.");while (isNaN(A) || !A===Math.floor(A)){	var AA = prompt ("Type in the maximum value of a and b");	var A = parseInt (AA, 10);	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);while (!x===0){	var X = prompt ("Type 0 to begin");	var x = parseInt (X, 10);	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

When you run this line:while (isNaN(A) || !A===Math.floor(A))The variable A has not been defined yet. For the second condition, when you use "!A" you are saying to use the opposite boolean value of A. If A is an integer greater than 0, then the expression "!A" will evaluate to boolean false. So you would be comparing boolean false against the return from Math.floor. If you're trying to test if two things are not equal, you use the !== operator.

Link to comment
Share on other sites

Try setting it before you start the loop.ie:var A = null; //if that doesn't work try var A = 'NaN';while (isNaN(A) || !A===Math.floor(A))Edit:You should actually declare all your variables outside your loops instead of inside them, ex:var A = null;var AA = '';while (isNaN(A) || !A===Math.floor(A))not:while (isNaN(A) || !A===Math.floor(A)) {var AA = prompt("Message");var A = parseInt(AA, 10);//...}

Link to comment
Share on other sites

I now hav this, but it still isn't even getting to the first 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 = parseInt (AA, 10);	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 = parseInt (X, 10);	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

I think it's the parseInt() method that's giving you unexpected results. When you put a value into the prompt, say 1.7, it returns it as a string, so you now have "1.7" as your value. parseInt("1.7") will return 1, since it is converting the string to an integer. So anytime you put in a decimal value its being converted to an integer and your condition (isNaN(A) || !(A===Math.floor(A))) will always be true because it's both a number and an integer.Try this instead:var A = Number(AA);

Link to comment
Share on other sites

But I still want to tell the program to keep repeating until A is an integer.
It will. All you have to change is the parseInt():
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); // <--- This line is all you need to change	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}

Link to comment
Share on other sites

on the input? just check its type, and if its not what you need from the user, reply back with another prompt box.

<script>prompt = function(){  var a = prompt("give me a number please");  if(isNaN(a)){	prompt();  //ask them again  }else{	process_response(a);  };};process_response = function(number){  var response = number;  //do what you want to do with the number};</script>onClick="prompt()"  //add as an event handler somewhere on your page

Link to comment
Share on other sites

on the input? just check its type, and if its not what you need from the user, reply back with another prompt box.
<script>prompt = function(){  var a = prompt("give me a number please");  if(isNaN(a)){	prompt();  //ask them again  }else{	process_response(a);  };};process_response = function(number){  var response = number;  //do what you want to do with the number};</script>onClick="prompt()"  //add as an event handler somewhere on your page

But you're overriding the original prompt() function with the other. You should rename the function.
Link to comment
Share on other sites

But you're overriding the original prompt() function with the other. You should rename the function.
oh, i think i see that...with this line: var a = prompt("give me a number please")?oh yeah, then rename the function :)
Link to comment
Share on other sites

... I'm a bit slow, and this is getting a tad confusing...Could someone tell me the definite code for getting an Integer number inputted into a prompt box, not letting the program leave until it is an integer number, and assigning it to a vaiable?

Link to comment
Share on other sites

OR just use this<!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 type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script> <style type="text/css"></style></head><body><script type="text/javascript">function isinteger(checkvalue){valid=true;if (isNaN(checkvalue) || parseFloat(checkvalue) !== parseInt(checkvalue,10)){valid=false;document.getElementById("errormsg").innerHTML="Invalid Value Entered Fool! - Must Be Integer";}else{document.getElementById("errormsg").innerHTML="";}return valid;}</script><form onsubmit="return isinteger(this.thisinput.value);" method="post"><input type="text" name="thisinput" onkeyup="isinteger(this.value);" onchange="isinteger(this.value);" /><input type="submit" value="submit" /></form><div id="errormsg"></div></body></html>

Link to comment
Share on other sites

... I'm a bit slow, and this is getting a tad confusing...Could someone tell me the definite code for getting an Integer number inputted into a prompt box, not letting the program leave until it is an integer number, and assigning it to a vaiable?
I did, in my last post. But I'll show you again.This is your original code:
var A = NaN;while (isNaN(A) || !(A===Math.floor(A))){	var AA = prompt ("Type in the maximum value of a and b");	var A = parseInt (AA, 10);	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}

All you need to do is change the line that reads var A = parseInt (AA, 10); to read var A = Number(AA); like this:

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); // <--------------------- Change this line, that's it.	if (isNaN(A) || !(A===Math.floor(A)))	{		window.alert (A + ' is not a number and/or is not an integer.');	}}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

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


×
×
  • Create New...