-3

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

rnm20
  • 5
  • 4
  • 1
    Could you not have simply edited your [previous question](http://stackoverflow.com/questions/15480801/im-having-an-issue-with-displaying-javascript-output-without-any-user-action) rather than creating an entirely new one for a very minor change? – James Donnelly Mar 19 '13 at 14:41
  • Really Sorry, my bad, honestly didn't pay attention to the edit button, won't happen again – rnm20 Mar 19 '13 at 14:46
  • 1
    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 – rnm20 Mar 19 '13 at 23:28

3 Answers3

2

You can use javascripts document.getElementById to get the textarea(of course you will have to give some id to the textarea). Then change value of this text area. Have a look at this answer.

Community
  • 1
  • 1
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • can the value be a string variable? i was thinking of building the primes in a string with for loop and in the loop: str+= i + "/n"; for example, then outputting the string in the text area, would that work? – rnm20 Mar 19 '13 at 14:58
  • @user2182981 in the very answer I link to the author sets that value to a string. – Ivaylo Strandjev Mar 19 '13 at 14:59
  • i tried it but it isnt working, i reposted the edited code above – rnm20 Mar 19 '13 at 15:07
0

You can't have HTML inside a textarea.

Bart
  • 17,070
  • 5
  • 61
  • 80
0

Here try the following code. Notice the updated textarea element, the placement of the JS reference and the output of the value.

JS File

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>");
          document.getElementById('primeNumbers').value = document.getElementById('primeNumbers').value + i + '\n'; 
      }
  }

}
printPrimes();

HTML

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1> Prime numbers between 1 and 1000 are: </h1>
        <textarea rows="10" cols="15" id="primeNumbers"></textarea>
        <script src="prime.js" type="text/javascript"> </script>
    </body>
</html>
Kalpers
  • 658
  • 5
  • 11
  • alright ill try this and get back to you – rnm20 Mar 19 '13 at 15:08
  • It worked :D thanks alot, it was a silly mistake i made, the script tag was above the textarea tag in my code so it was running before the text area was made :p..thanks alot for the code :D – rnm20 Mar 19 '13 at 15:14
  • Can you flag this answer as useful? Thanks – Kalpers Mar 19 '13 at 16:03