Js basic lecture

im back again… :sweat_smile:
I thing is better to point out all the problem i have in this topic before i create a new topic each time i have a problem so plz bear with my question … :smile:

#20- Exercise- Prime Numbers


function showPrimes(limit) {
    for (let number = 2; number <= limit; number++){
        //  1st loop 2 -20  inside the first loop runs simultaneously 2nd loop 2-20
      // if loop 1 % loop 2 === 0 then isPrime is false. after that my brain went afk lol                   
        let isPrime = true;
        for (let factor = 2; factor < number; factor++) {
            if (number % factor === 0) {
                isPrime = false;

        if (isPrime) console.log(number);


the code from the exercise works just fine but
again i dont have a clue how this code works.
much thanks in advance for the reply.
btw the exercises are pretty tough …

The way it works is considering a number is prime by default and then try to find a counter example. If so it is enough to say the number is not prime anymore.

ok ive thought about it and i hope got it right.

loop1 send out number 3.
loop 2 checks if loop1 number 3 can % with number 2 ===0 its false so return true. if its true it return false back.
on the first loop console.log if loops2 send out true it does so console.log number 3.

loop1 send out number 4.
loop 2 checks if number 3 from the loop1 can % with number 2 ===0 its true so return false.
on the first loop console.log if loops2 send out true it doesn`t so no console.log number 4.
and so on until <= limit.

if i got this right. the next question is why is there number 2 in html console.

loop1 send out number 2.
loop 2 checks if number 2 from the loop1 can % with number 2 ===0 its true so return false.
on the first loop console.log if loops2 send out true it doesn`t so no console.log number 2.

it shouldn`t console.log 2

ok now i got it isPrime give default = true
phew at last i now can go to bed . Thanks again for the advice :smiley:

I’m more of a backend guy than front end but I can suggest you use the debugger to be able to observe what your code does bit by bit.

For that you simply need to add the debugger statement which will trigger the debugging tools. Then on the top right you can see the different buttons to go through the code. I did try the F10 button and it does go through the code too.

On the right you have the different values in the available scopes.

Edit: Had fun with your code.

cool thanks for the tip with the debugger mode
helps me more to understand how it works :smiley:

1 Like

I remember how much i got confused when i faced this algorithm for the first time, so i saved it’s explanation from the previous forum (RIP) :slight_smile:

basic course done
now im at part 2 and the first exercise well how the hell should i know how to code this -.- lol anyway ive watch the solution and i m not sure if i got this right hope you guys can help me out.

const sw = new Stopwatch()

function Stopwatch() {

    let startTime, endTime, running, duration = 0

    this.start = function () {
        if (running)
            throw new Error("Stopwatch has already started.")

        running = true

        startTime = new Date();   //get the actual date with time correct ?

    this.stop = function () {
        if (!running)  // how should i interpret this ? if running is not false execute. if false throw exception ?
            throw new Error("Stopwatch has not started.")

        running = false

        endTime = new Date()

        const seconds = (endTime.getTime() - startTime.getTime()) / 1000;
        duration += seconds;  // .getTime() with this you get the actual time from the new Date () ?

    this.reset = function () {
        startTime = null
        endTime = null
        running = false
        duration = 0;

    Object.defineProperty(this,"duration", {
        get: function () { return duration }


getTime() should return the time in ms so why /1000 ?
ive read through but i was not sure so hope you guys can clarify for me .
thanks in advance

Milliseconds divided by 1000 converts to seconds. For example, 2000ms is 2 seconds (2000/1000).

ah ok now i got it
like 23000 ms with /1000 = 23,000 sek
thanks for explaining it to me

1 Like