this is the Question: An integer is said to be prime if it is greater than 1 and divisible only by 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a) Write a function that determines whether a number is prime. b) Use this function in a script that determines and prints all the prime numbers between 1 and 10000. How many of these 10000 numbers do you really have to test before being sure that you have found all the primes? Display the results in a .
This is my code:
function isPrime(n)
{
var prime=true;
if(n===1 || n===0)
{
prime= false;
}
if(n==2)
{
prime= true;
}
else
{
for(var i=2;i<n;i++)
{
if(n%i===0)
{
prime= false;
}
}
}
return prime;
}
function printPrimes()
{
for(var i=0; i<=1000; i++)
{
if(isPrime(i)===true)
{
document.writeln("<p>" + i + "</p>");
}
}
}
printPrimes();
This is My html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1> Prime numbers between 1 and 1000 are: </h1>
<textarea rows="10" cols="15">
<script src="prime.js" type="text/javascript"> </script>
</textarea>
</body>
</html>
When i open the html file on chrome only the header shows up the script doesnt seem to run! and only the script tag shows up as is in the textarea shows up in the text area, i tried a million things nothing works, how am i suppose to output the script in the TEXTAREA tag??????
I tried This but still no go:
function printPrimes()
{
var str="";
for(var i=0; i<=1000; i++)
{
if(isPrime(i)===true)
{
str+=i +" /n";
}
}
document.getElementById('primes').value=str;
}
printPrimes();
html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
` <script src="prime.js" type="text/javascript"> </script>
<h1> Prime numbers between 1 and 1000 are: </h1>
<textarea id="primes" rows="40" cols="50">
</textarea>
</body>
</html>
So Sorry for the repost and slight edit, will not happen again, im new to stack overflow and got banned already, if my ban can be lifted, i will only ask more questions if i have already done research and if i hit rock bottom and nothing seems to be working. I ddnt notice the edit post, it was a silly mistake to post a duplicate, hope the down votes get removed, im s script newbie, and stack overflow newbie, i ddnt realize id get banned for silly questions. sorry