Forum Moderators: open
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!
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?
<!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>
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(", "));
}
[webmasterworld.com...]