My prime number function is broken but I don't know why

user4509200

The following is a small function that I made that is supposed to list all of the prime numbers between 1 and 1001. It uses a for loop to get a number, then another for loop to check if it is prime. Once it is done checking (if it is prime), it pushes the number to an array. This array should be outputted at the end, but it is not. Thanks in advance.

<html>
<body>

<script>

function pNumList(){
    var primeNumbers = [];

    for(var noomber = 2; noomber<=1001; noomber++){
        for(var i = 2; i<noomber; i++){
            if(noomber%i==0){}else{


                primeNumbers.push(noomber);
            }
        }
    }
    pNumbersList.innerHTML = primeNumbers;
}

</script>

<p id="pNumbersList"></p>
<button  onclick="pNumList()" value = "Let's see em">Prime numbers list!</button>
<br>
<p>This might take a sec...</p>

</body>
</html>.
jcubic

Here is working code:

function pNumList(){
    var primeNumbers = [];

    for(var noomber = 2; noomber<=1001; noomber++){
        var prime = true;
        for(var i = 2; i<noomber; i++){
            if(noomber%i==0){
                prime = false;
                break;
            }
        }
        if (prime) {
            primeNumbers.push(noomber);
        }
    }
    pNumbersList.innerHTML = primeNumbers.join(',');
}

JSFIDDLE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

My is_prime function fails on 9, and I don't know why?

From Dev

My page is broken, I don't know why their is a horizontal scroll when there should be none

From Dev

My code works but I don't know why

From Dev

I don't know why my image is not showing on the frame

From Dev

I don't know why my structs below are not being set

From Dev

My query returns null in wordpress and I don't know why

From Dev

I don't know why my program is not entering the loop

From Dev

I don't know my Database name

From Dev

I'm using foundation, and I don't know why my header isn't centered correctly

From Dev

My PHP row array is giving me duplicates in my table, and I don't know why

From Dev

I don't know why this canvas is null

From Dev

Sound is gone, I don't know why

From Dev

title is indented and i don't know why

From Dev

SMTP Error, I don't know why

From Dev

I don't know how to make this function

From Dev

My nested for loop isn't executing and I don't know why

From Dev

R - I don't know why my code won't run?

From Dev

I don’t know why my string can't save the value

From Dev

Need some help in my C program,I've got a segmentation fault but I don't know why

From Dev

I'm trying to answer Hackeranks data structure question and i don't know why this function fails

From Dev

I don't know why my tracking beacon isn't being sent as I debug my Google Analytics

From Dev

I don't know why my tracking beacon isn't being sent as I debug my Google Analytics

From Dev

I don't get why my .find function don't work

From Dev

I don't know why my cassandra has such a large hints table?

From Dev

In my 7 bit adder the carry bit is incorrect and i don't know why

From Dev

For some reason my code adds 2 to a variable instead of 1 and I don't know why

From Dev

For some reason my code adds 2 to a variable instead of 1 and I don't know why

From Dev

My c++ application fails to link, and i don't know why

From Dev

i don't know why this code is writing empty rows on my csv file when using a PC but not on a Mac

Related Related

  1. 1

    My is_prime function fails on 9, and I don't know why?

  2. 2

    My page is broken, I don't know why their is a horizontal scroll when there should be none

  3. 3

    My code works but I don't know why

  4. 4

    I don't know why my image is not showing on the frame

  5. 5

    I don't know why my structs below are not being set

  6. 6

    My query returns null in wordpress and I don't know why

  7. 7

    I don't know why my program is not entering the loop

  8. 8

    I don't know my Database name

  9. 9

    I'm using foundation, and I don't know why my header isn't centered correctly

  10. 10

    My PHP row array is giving me duplicates in my table, and I don't know why

  11. 11

    I don't know why this canvas is null

  12. 12

    Sound is gone, I don't know why

  13. 13

    title is indented and i don't know why

  14. 14

    SMTP Error, I don't know why

  15. 15

    I don't know how to make this function

  16. 16

    My nested for loop isn't executing and I don't know why

  17. 17

    R - I don't know why my code won't run?

  18. 18

    I don’t know why my string can't save the value

  19. 19

    Need some help in my C program,I've got a segmentation fault but I don't know why

  20. 20

    I'm trying to answer Hackeranks data structure question and i don't know why this function fails

  21. 21

    I don't know why my tracking beacon isn't being sent as I debug my Google Analytics

  22. 22

    I don't know why my tracking beacon isn't being sent as I debug my Google Analytics

  23. 23

    I don't get why my .find function don't work

  24. 24

    I don't know why my cassandra has such a large hints table?

  25. 25

    In my 7 bit adder the carry bit is incorrect and i don't know why

  26. 26

    For some reason my code adds 2 to a variable instead of 1 and I don't know why

  27. 27

    For some reason my code adds 2 to a variable instead of 1 and I don't know why

  28. 28

    My c++ application fails to link, and i don't know why

  29. 29

    i don't know why this code is writing empty rows on my csv file when using a PC but not on a Mac

HotTag

Archive