/    Sign up×
Community /Pin to ProfileBookmark

I Need Help Please

I Have to write a program in javascript to print 1000 prime numbers using and Array[] and a “for(in) loop” and i have come up with a complete brain block it has been years since i have messed with javascript.I have to do this for a college assignment in web programming. if someone could help me out i would be greatly appreciated….Heres what I have so far i don’t think its even close

<html>
<head>
<title>Untitled</title>
<script>

</script>
</head>

<body>
<script language =”JavaScript”>
var Prime = new Array;// the Array destined to hold the prime numbers
Prim3 [0]=2
var Num =3 //number being checked
var count ; // general counter
for (count =0;count<= 1000;count++){

}
for ( var i = 2; i < NumPrim[count]; i++){
NumPrim[count]++
}
document.write(NumPrim[count] + “<br>”)

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

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterSep 23.2005 — It's your choice to "cheat" on your homework, hopefully you will pick-up some good coding skills from my work.
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 STRICT//EN" "http://www.w3.org/TR/HTML401/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function getPrimeNumbers(stopPoint){
var primeNumbersArray=[];
mainloop:for(var i=2,j;true;i++){
for(j=0;j&lt;primeNumbersArray.length;j++)if(i%primeNumbersArray[j]==0)continue mainloop;//not a prime number
primeNumbersArray[primeNumbersArray.length]=i;if(primeNumbersArray.length&gt;=stopPoint)break mainloop;
}
return primeNumbersArray;
}

var primeNumbers=getPrimeNumbers(1000);
document.write(primeNumbers.join("&lt;br&gt;"))
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@UltimaterSep 23.2005 — A for(in) loop you want, sorry, here you go:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 STRICT//EN" "http://www.w3.org/TR/HTML401/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function getPrimeNumbers(stopPoint){
var primeNumbersArray=[];
mainloop:for(var i=2,j;true;i++){
for(j in primeNumbersArray)if(i%primeNumbersArray[j]==0)continue mainloop;//not a prime number
primeNumbersArray[primeNumbersArray.length]=i;if(primeNumbersArray.length&gt;=stopPoint)break mainloop;
}
return primeNumbersArray;
}

var primeNumbers=getPrimeNumbers(1000);
document.write(primeNumbers.join("&lt;br&gt;"))
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@David_HarrisonSep 23.2005 — Come on Ultimator, he wasn't asking for done and dusted code, just some help with homework.

Besides, your code is flawed in that it doesn't use a for(in) loop and that document.write should be in the <body> section.

Perhaps this link will help a bit:

http://www.w3schools.com/js
Copy linkTweet thisAlerts:
@UltimaterSep 23.2005 — Real funny.
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 STRICT//EN" "http://www.w3.org/TR/HTML401/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function getPrimeNumbers(stopPoint){
var primeNumbersArray=[];
mainloop:for(var i=2,j;true;i++){
for(j in primeNumbersArray)if(i%primeNumbersArray[j]==0)continue mainloop;//not a prime number
primeNumbersArray[primeNumbersArray.length]=i;if(primeNumbersArray.length&gt;=stopPoint)break mainloop;
}
return primeNumbersArray;
}

var primeNumbers=getPrimeNumbers(1000);
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
&lt;script type="text/javascript"&gt;
document.write(primeNumbers.join("&lt;br&gt;"));
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@mr_ratauthorSep 23.2005 — It's your choice to "cheat" on your homework, hopefully you will pick-up some good coding skills from my work.

[/code][/QUOTE]

I'm sorry you misunerstood me i was jsut wanting examples of how to do it ....i rewrote the code way different and yes i did learn alot it makes since to me now to where as before it didnt..i don't cheat...i have too much pride for thAT and that assumption wasn't fair or justified...but thank you for helping me understand something that was confusing.
Copy linkTweet thisAlerts:
@UltimaterSep 23.2005 — Just to make it clear, putting words in quotation marks generally is a way to exaggerate.

In other words, my intention on-behalf-of what I stated was as follows:

[color=royalblue]It's your choice to [color=#333333][b]seek help[/b][/color] on your homework, hopefully you will pick-up some good coding skills from my work.[/color]

I wasn't making any assumption as to you cheating, and it wouldn't bother me even if you were. I apologize for offending you and I admit it would have been better if i put "seek help" instead of "cheat".

If you need any help understanding my code, I'm very good at explaining and giving examples -- don't be "shy" and miss the opportunity if you don't fully understand something or another. I'm quite sure I used at least one instance of some coding that you didn't understand.
×

Success!

Help @mr_rat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...