4 Replies - 32708 Views - Last Post: 04 April 2008 - 01:30 PM

#1 nbcaznmaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 08

Javascript, Finding Prime Numbers WHile loops

Posted 04 April 2008 - 12:48 PM

<html>
	<head>
		<title>Javascript4#1</title>
	</head>
	<body>
		<script type="text/javascript">
		var a="prompt("Type number here.","");
		var i=2;
		var b;
		var c="true";
		while(i<a)
		{
			b=a/i;
			if(b=0)
			{
				c="false";
			}
			1++;
		}
		if(c="false")
		{
			alert("Your number is not a prime number");
		}
		else
		{
			alert("Your number is a prime number");
		}
		</script>
	</body>
</html>



I have the loop that is the basis for weeding out whether it is prime or not, but it is difficult to determine how to output an alert that shows whether it is prime or not.

Is This A Good Question/Topic? 1

Replies To: Javascript, Finding Prime Numbers WHile loops

#2 capty99  Icon User is offline

  • i am colt mccoy
  • member icon

Reputation: 98
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Javascript, Finding Prime Numbers WHile loops

Posted 04 April 2008 - 01:15 PM

if(c=="false")



I THINK.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Javascript, Finding Prime Numbers WHile loops

Posted 04 April 2008 - 01:19 PM

<script type="text/javascript">
		var a=prompt("Type number here.","");
		var i=2;
		var b;
		var c = true;
		while(i<a)
		{
			b=a%i;
			if (b==0)
			{
				c = false;
				break;
			}
			i++;
		}

		if (c)
		{
			alert("Your number is a prime number");
		}
		else
		{
			alert("Your number is not a prime number");
		}
</script>



Give that a whirl. Some of the problems you had was that....

1. You were using a single "=" when you should be using two "==". Remember that you use = for assignment and == for comparisons.

2. Just assign true and false to the variable "c" not strings that say "true" or "false". That way when it comes to your if statement you can use if because the variable is an actual boolean.

3. You had a typo where you were using 1++ when you mean i++

4. You had a double quotation mark before the word "prompt" which shouldn't have been there.

5. Lastly you want to use a % i. (the modulus operator). This will return the remainder of a division. If it divides evenly then it will have a remainder of 0. Otherwise 6 / 3 would = 2 and say that it is a prime number because it isn't 0. 6 % 3 on the other hand has a remainder of zero thus is not a prime number.


See if that works for you. :)

This post has been edited by Martyr2: 04 April 2008 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 nbcaznmaster  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 08

Re: Javascript, Finding Prime Numbers WHile loops

Posted 04 April 2008 - 01:26 PM

Oh, but whatis the point of the "break"? Also, why doesnt it say if(c==true)? Also, i think i meant to put 1*prompt(), is this syntactically correct? I remember having to do this in order to delcare that teh input is a number rather than a string

This post has been edited by nbcaznmaster: 04 April 2008 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • Joined: 18-April 07

Re: Javascript, Finding Prime Numbers WHile loops

Posted 04 April 2008 - 01:30 PM

The point of the "break" is that it causes the while loop to end early. once we find it is not a prime, there is no use continuing the loop.

And the reason you don't see (c == true) is that your variable "c" is a boolean variable which means that it evaluates to true or false. That is why I said don't assign the string "true" or "false" to it, just assign true or false. This makes the variable a boolean type.

If you need more explanation of booleans, just lookup the term "boolean datatype" or "bool datatype". Since it is a boolean that translates to true or false, it doesn't need to be compared. A comparison just evaluates to a boolean value itself. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1