Forum Moderators: open

Message Too Old, No Replies

Javascript Alert for Series of Prime Numbers

         

emmax64

12:06 am on Jul 15, 2008 (gmt 0)

10+ Year Member



I'm very need to Javascript and I have been working days on this problem. I need to list the first ten prime numbers in a series of ten alert messages when a button is pressed on the web page.

This is the code I have so far, which gives me the button but I continue to have problems with getting the ten alert messages to come up.

<html>
<head>

<script type="text/javascript">
<!--

var primeString=''
var primeCount=0

function checkprime(){

while (primeCount<=10){

var isprime=1

for (j=2; j<=Math.sqrt(i); j++)
{

if(i%j)==0)
{
isprime=0
}
}

primeCount=(isprime==1?primecount=1:primecount)
if (isprime==1){primeString=primeString=i}
i=i=1}
}
document.write(primeString)
}
//-->
</script>
</head>

<body>
<h2>The First Ten Prime Numbers</h2>
<h3>Click to find the first ten prime numbers</h3>

<form name="PrimeNumbers">
<input name="btnprompt"
id="btnprompt"
type="submit"
value="Find Primes"
onClick="Display_Prime()">
</form>

</body>
</html>

Thanks in advance!

LifeinAsia

12:14 am on Jul 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Change
document.write(primeString)
to
alert(primeString)

However, having 10 alerts that you have to click and close (for each one) is VERY annoying.

emmax64

1:09 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Thanks for your help!

I agree about the 10 alerts, but it is what was requested.

emmax64

3:05 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Okay, I tried the alert(primeString) but i still receive error message that syntax is wrong. I continue to try several things. I believe I need to use two functions, one to run the prime numbers to get the first ten primes and another function to generate the individual alert messages for each of the the first ten prime numbers. I want the user to be able to press the button and the whole process begins.

This is where I'm at....

<html>
<head>

<script type="text/javascript">
<!--

var primeString=''
var primeCount=0

function checkprime(){

var i=1

while (primeCount<=10){

var isprime=1

for (j=2; j<=Math.sqrt(i); j++)
{

if(i%j==0)
{
isprime=0
}
}

primeCount=(isprime==1?primecount=1:primecount)
if (isprime==1){primeString=primeString+i}
i=i+1
}
function display_prime(){
alert(primeString);
}

</script>
</head>

<body onload="checkprime()">
<h2>The First Ten Prime Numbers</h2>
<h3>Click to find the first ten prime numbers</h3>

<input type="button"
name="btnDo"
id="btnDo"
value="Find Prime"
onClick="display_prime()">

</body>
</html>

This still doesn't work....any help?

LifeinAsia

3:36 pm on Jul 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Probably easier to just brute force it:
function checkprime(){
alert(1);
alert(2);
alert(3);
alert(5);
alert(7);
alert(11);
alert(13);
alert(17);
alert(19);
alert(23);
}

Fotiman

6:04 pm on Jul 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!--
Note the strict DOCTYPE. It's good practice to use a complete DOCTYPE to
put browsers in standards compliant mode (vs. quirks mode).
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Untitled</title>
</head>
<body>
<h2>The First Ten Prime Numbers</h2>
<h3>Click to find the first ten prime numbers</h3>
<form action="">
<div>
<!-- forms can only contain block elements, so this is wrapped in a div -->
<input type="button" id="btnDo" value="Find Prime">
</div>
</form>
<!--
Note, there are no HTML comments in my script elements. They are not
needed and haven't been since Netscape 1. Also note that I am attaching my
event handlers in the script instead of in my markup... unobtrusive. :)
-->
<script type="text/javascript">
window.onload = function() {
// Attach listener to button
var btnDo = document.getElementById('btnDo');
/**
* Check a number to see if it is prime. Returns false for numbers less
* than 2.
* @param {Integer} n The number to check
* @return true if the number is divisible by only itself an 1, otherwise false
*/
function isPrime(n) {
var i;
if (n < 2) { // 1, 0, and negative numbers are not prime.
return false;
}
for (i = n - 1; i > 0; i--) {
if (i == 1) {
return true;
}
if (n % i == 0) {
return false;
}
}
}
btnDo.onclick = function() {
// Get the first 10 prime numbers starting at 2. Adjust n to start
// higher.
var n, nPrime = [];
for (n = 2; nPrime.length < 10; n++) {
if (isPrime(n)) {
nPrime[nPrime.length] = n;
}
}
// nPrime is now an array of 10 prime numbers
alert('Prime Numbers:\n' + nPrime.join(", "));
}
};
</script>
</body>
</html>

emmax64

9:16 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Thank you Fotiman!

Your code give me one alert message with the first ten prime numbers in. How could the code be adjusted to print each of the first ten prime numbers in ten separate alert messages. Opening one after the other?

Fotiman

9:28 pm on Jul 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




btnDo.onclick = function() {
// Get the first 10 prime numbers starting at 2. Adjust n to start
// higher.
var n, nPrime = [];
for (n = 2; nPrime.length < 10; n++) {
if (isPrime(n)) {
nPrime[nPrime.length] = n;
alert('Prime Number:\n' + n);
}
}
// nPrime is now an array of 10 prime numbers
// alert('Prime Numbers:\n' + nPrime.join(", "));
}

emmax64

11:25 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Wow! Excellent! Thank you so much!

Your code descriptions were very helpful!

httpwebwitch

1:46 am on Jul 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



remember, prime numbers are never even so when you're looping though numbers you can increment by 2. That will make your script twice as fast

well, all the primes except 2 itself

you know what I mean

httpwebwitch

1:50 am on Jul 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



there was a nice - and optimized - "isprime" function in another thread earlier this year

[webmasterworld.com...]