# Js basic lecture

**URL:** <https://forum.codewithmosh.com/t/js-basic-lecture/14528>\
**Category:** JavaScript\
**Created:** [August 18, 2022, 6:09pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528 "2022-08-18T18:09:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nguyen](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nguyen](https://forum.codewithmosh.com/u/Nguyen)\
**Post date:** [August 18, 2022, 6:09pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/1 "2022-08-18T18:09:45Z")

</div>

hi  
im back again… 😅  
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 … 😄

#20- Exercise- Prime Numbers

```auto
showPrimes(20)

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;
                break;
            }

        }
        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 …

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [August 18, 2022, 8:29pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/2 "2022-08-18T20:29:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nguyen](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nguyen](https://forum.codewithmosh.com/u/Nguyen)\
**Post date:** [August 18, 2022, 9:53pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/3 "2022-08-18T21:53:58Z")

</div>

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  
edit

> [@UniqueNospaceShort](#):
>
> 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 now i got it isPrime give default = true  
phew at last i now can go to bed . Thanks again for the advice 😃

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [August 19, 2022, 1:01pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/4 "2022-08-19T13:01:39Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/2/296c0ed9bcc933830d42b0452296b0660cb5442a.png)

Edit: [Had fun with your code](https://jsfiddle.net/UchiTesting/syfnzh6a/3/).

---

<div class="post-metadata">

**Author:** ![Nguyen](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nguyen](https://forum.codewithmosh.com/u/Nguyen)\
**Post date:** [August 23, 2022, 10:58am UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/5 "2022-08-23T10:58:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![A100D-JS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/a100d-js/32/8_2.png) [@A100D-JS](https://forum.codewithmosh.com/u/A100D-JS)\
**Post date:** [August 25, 2022, 9:05pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/6 "2022-08-25T21:05:09Z")

</div>

Hi,  
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) 🙂

 ![Prime Number Explained](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/8/80cce00a0eb818eef609c78a007ae509aa85ee9f.png)

---

<div class="post-metadata">

**Author:** ![Nguyen](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nguyen](https://forum.codewithmosh.com/u/Nguyen)\
**Post date:** [September 3, 2022, 5:00pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/8 "2022-09-03T17:00:44Z")

</div>

Hi  
basic course done  
now i`m 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.

```auto
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 }
    })

}

```

> **[Date - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date?retiredLocale=de)**
>
> JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

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

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 3, 2022, 7:49pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/9 "2022-09-03T19:49:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nguyen](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@Nguyen](https://forum.codewithmosh.com/u/Nguyen)\
**Post date:** [September 5, 2022, 2:41pm UTC](https://forum.codewithmosh.com/t/js-basic-lecture/14528/10 "2022-09-05T14:41:19Z")

</div>

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