Felix Smith
Felix Smith

Reputation: 3

Problem while trying to find prime numbers between 2 and 100

I am having trouble finding the problem with my short program, meant to find all the prime numbers between 2 and 100. I have tried rewriting the program several times; but my browser always slows to a halt when I try to run it. Any help would be much appreciated!

        function is_prime(x)
        {
            for (i = 2; i <= x / 2; ++i)
                if (x % i == 0) return false;
            return true;
        }

        for (i = 2; i < 100; ++i)
            if (is_prime(i)) console.log(i);

Upvotes: 0

Views: 48

Answers (1)

Ahmed Gaafer
Ahmed Gaafer

Reputation: 1661

The i in your loops are not declared

 function is_prime(x)
        {
            for (let i = 2; i <= x / 2; ++i)
                if (x % i == 0) return false;
            return true;
        }

for (let i = 2; i < 100; ++i)
    if (is_prime(i)) console.log(i);

Upvotes: 1

Related Questions