# Hey cant work out whats wrong with my prime code, wondered if you can help

Printable View

• 11-24-2012, 08:54 AM
k1ngok1ngs
Hey cant work out whats wrong with my prime code, wondered if you can help
The code is meant to have two functions. the first to determine if an integer is prime the second to show all prime numbers between two integers. so far my first function works fine but I'm getting the error message "Uncaught TypeError: Property 'prime' of object [object Window] is not a function primefeat2-2.html:30" for the second function. cant for the life of me work out whats wrong would appreciate some help :) I will collaborate them to be two separate options after I have worked out the basic code.

<html>
<body>
<script type="text/javascript">

function prime(){

var count = 2;
var stop = Math.ceil(Math.sqrt(number1));
var remainder = number1 % count;

while(count < stop){
number1 % count;
if (remainder == 0){
prime = false;
break;
}
else{
prime = true;
}
count ++;
}
}

function feature2(){

var number1 = window.prompt("enter a number","0");
var number2 = window.prompt("enter a number","0");

while (number1 < number2){
if (prime(number1)){
document.writeln(+ number1 + " is prime");
}
number1++;
}

}

feature2();

</script>
</body>
</html>

thanks.
• 11-24-2012, 04:25 PM
007Julien
Your code is wrong. It define a function prime, but this function is killed with an assignment of boolean (true or false) to prime !

See this w3schols.com page about functions and particularly Functions with a return value. Then you could use arguments...

Then, it should calculate the rest in the while loop...
• 11-25-2012, 07:21 AM
k1ngok1ngs
Cheers
Quote:

Originally Posted by 007Julien
Your code is wrong.

I worked it out and improved it a great deal so it works. Now i have to create another feature to work out the primes between 2 numbers. wish me luck (: got 3 days left
• 11-25-2012, 09:30 AM
007Julien
Do not use document.write (or writeln) which is made to write on an open document (at the opening of the page) but never when the document is fully loaded.

Use rather document.getElementById('rsp') in a script at the bottom of the body or after a onload event (the document should this time be present) to insert some html text with innerHTML with something like following.

Then its possible to avoid many divisions with only odd integers or the preceding primes which can be store in an array...

Code:

```<body>   <div id="pge"></div> <script type="text/javascript"> // An object var Prm={         // with properties          nbr:null,prm:[2],rng:1,         // Initialization         ini:function(){Prm.nbr=3;Prm.prm[Prm.rng++]=3;                 document.getElementById('pge').innerHTML='2, 3';                 // The following !                 Prm.svt();},         // Search with odd integers                  svt:function(){                 Prm.nbr+=2;                 if (Prm.tst()) {document.getElementById('pge').innerHTML+=', '+Prm.nbr;                         Prm.prm[Prm.rng++]=Prm.nbr;}                 // The first 1000                        if (Prm.rng<1000) setTimeout(Prm.svt,3);         },         // Prime test with the preceding primes         tst:function(){var i=0,j,m=~~Math.sqrt(Prm.nbr);                 while(Prm.prm[i]<=m && (j=Prm.nbr%Prm.prm[i])!=0) i++;                 return j!=0;         } } Prm.ini(); </script> </body>```
Other methods test only 6 x k +-1 integers. See other threads of is forum and particularly this rndme function isPrime.

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.09934 seconds
• Memory Usage 2,367KB
• Queries Executed 11 (?)
More Information
Template Usage (21):
• (1)ad_footer_end
• (1)ad_footer_start
• (1)ad_global_above_footer
• (1)ad_global_below_navbar
• (1)ad_global_header1
• (1)ad_global_header2
• (1)ad_navbar_below
• (1)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)header
• (1)headinclude
• (1)headinclude_bottom
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)printthread
• (4)printthreadbit
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
• showthread
Included Files (19):
• ./printthread.php
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/functions_navigation.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_facebook.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_threadinfo_query
• fetch_threadinfo
• fetch_foruminfo
• load_show_variables
• load_forum_show_variables
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• printthread_start
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• printthread_post
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• fetch_musername
• notices_check_start
• notices_noticebit
• friendlyurl_resolve_class
• friendlyurl_clean_fragment
• friendlyurl_geturl
• process_templates_complete
• printthread_complete
• page_templates