/    Sign up×
Community /Pin to ProfileBookmark

window.prompt positioning help with prime function

Hey i was asked to write two programs. one that determines if an integer is a prime and another to state all the primes between two integers. I have written the first and it works perfectly fine (if not a little over complicated) but I’m having trouble with the second.
I worked out the basic algorithm for it but i cant for the life of me work out how to incorporate two window.promt() into the code without messing it up. there meant for a low and high number for the user to input before receiving all the primes in between those numbers. Here is the basic code.

<html>
<body>
<script language=”javascript”>

l=1000;n=3;p=1;
primes = new Array()
primes[0]=2;
while (n<=l){q=1;
while (primes[q]<=Math.floor(Math.sqrt(n)) && n/primes[q]!=Math.round(n/primes[q])){q++;}
if ((n-1)/1000==Math.round((n-1)/1000)){window.status=”Numbers checked so far: “+(n-1);}
if (n/primes[q]!=Math.round(n/primes[q])){primes[p]=n;p++;}
n+=2;}
document.write (“What follows next are prime numbers:<br>”+primes.join(“, “));
</script>
</body>
</html>

I apologise for how messy it is. help would be appreciated I wont have any hair left to pull out soon

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZABINov 26.2012 — why don't you use forms instead of window.prompt ?
Copy linkTweet thisAlerts:
@007JulienNov 26.2012 — Do not use document.write ! You will be obliged to reload the page to rerun the script.

Build your page with the following scheme
[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>Prime numbers</title>
<style type="text/css">
input{width:40px;text-align:center}
</style>
</head>
<body>
<h1>Prime numbers</h1>

[COLOR="#0000FF"]<!-- a container for the first response -->[/COLOR]
<div id="rsp"></div>

[COLOR="#0000FF"]<!-- three inputs for entries and a button-->[/COLOR]
<p>Displaying the primes between to intergers</p>
<label>Min value <input id="mnv" type="text" value=""></label>
<label>Max value <input id="mxv" type="text" value=""></label>
<input type="button" value="Ok" onclick="calcPrim()">
[COLOR="#0000FF"]<!-- a new container for the second response -->[/COLOR]
<div id="rsq"></div>

[COLOR="#0000FF"]<!-- The script at the end of the body with a type instead of a language -->[/COLOR]
<script type="text/javascript">

[COLOR="#0000FF"]// Your script[/COLOR]
l=1000;n=3;p=1;
primes = new Array()
primes[0]=2;
while (n<=l){q=1;
while (primes[q]<=Math.floor(Math.sqrt(n)) && n/primes[q]!=Math.round(n/primes[q])){q++;}
if ((n-1)/1000==Math.round((n-1)/1000)){document.getElementById('rsp').innerHTML=<p>"Numbers checked so far: "+(n-1)+"</p>";}
if (n/primes[q]!=Math.round(n/primes[q])){primes[p]=n;p++;}
n+=2;}
document.getElementById('rsp').innerHTML="What follows next are prime numbers:<br>"+primes.join(", ");

[COLOR="#0000FF"]// A function to read the values and to display the result[/COLOR]
function calcPrim(){
[COLOR="#0000FF"]// the + sign convert the values to numbers[/COLOR]
var min=+document.getElementById('mnv').value;
var max=+document.getElementById('mxv').value;
[COLOR="#0000FF"]// A first test to improve (see isNaN)[/COLOR]
if (!min || !max) {alert("Please fill the min and max values !");return}

[COLOR="#0000FF"]// the calculation[/COLOR]

[COLOR="#0000FF"]// Dislaying the results[/COLOR]
document.getElementById('rsq').innerHTML="<p><i>I have only to write the script to list the primes between "+min+" and "+max+".</i></p>";
}
</script>
</script>
</body>
</html>[/CODE]
×

Success!

Help @k1ngok1ngs 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.18,
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,
)...