Alright. This is a tough one.
We want to ask the user for an input. Then we want to print out every prime number that comes before the userInp.
Hmmm
A prime number is anything that can only be divided by its self, and one.
That means that it can not be cut in half, basicaly.
Which also means, that if it has a Modulo its remainder would be 0. Right?
%2
hmmm.
First. I need to get this userInp.
PROMPT
var userInp = prompt("Yo, Diggidy, Give me a number", "");
Lets parse
var userNum = parseInt(userInp);
Now lets see if our userNum was cut in half, would it cut evenly?
if (userNum % 2 == 0){
prompt("Sorry, this is NOT a prime number!");
} else {
prompt("Yo this IS a prime number!");
}
:D
We know what numbers are. How do we print them out!
*** Pseudo Coding ***
If my number is a prime number, I need to take every other prime number that is less of it, and print it.
So that means, every number that has a % 2 == 0
I want to print.
So someway I need to say that any number that is before userNum, needs to see if it is dividable by two. If it is, print.
!!Also, I need to Loop through each number to see if its modulo is 0.
For loop.
I need to make sure that my 4 loop goes as many times as my User num, so I can get each number before it.
for (aNum = 1; aNum < userNum; aNum++ ){
}
What do we want to happen.
we want to check the Modulo thing. Well, we already did that in our If Statement.
Lets LOOK! inside our loop and see what happens.
The first line...
if(userNum % 2 ==0){
!!Dont do anything
} else {
//We now want to print!
EVERY NUMBER THAT
Cannt be divided by two and equal zero!
}
if (userNum % 2 == 0){
prompt("Sorry, this is NOT a prime number!");
} else {
prompt("Yo this IS a prime number!");
Break Time!!!!
I need to think about this a little more!!!
var userInp = prompt("Enter a number.");
var userNum = parseInt(userInp)
for (var aNum = 0; aNum <>
//document.write("blahblah");
if (userNum % 2 == 0){
//document.write(userNum);
alert("Sorry, this is not a correct Prime Number.");
break;
} else if (userNum % 2 != 0){
document.write(aNum + " Is a prime number." + "
");
}
}
Saturday, August 2, 2008
Subscribe to:
Post Comments (Atom)

1 comment:
you know for a fact that 1, 2 and 3 are prime numbers, so you don't have to check those. just print them.
another hint: Beyond number 3, A number is not a prime number if it can be divided
exactly by one of the integers between 2 and the square root of this number (given by the user).
Post a Comment