# JavaScript Pt2 - Prototypes Solution doesn't work

**URL:** <https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886>\
**Category:** JavaScript\
**Created:** [February 21, 2021, 9:34am UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886 "2021-02-21T09:34:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cage](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/cage/32/990_2.png) [@Cage](https://forum.codewithmosh.com/u/Cage)\
**Post date:** [February 21, 2021, 9:34am UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/1 "2021-02-21T09:34:04Z")

</div>

I just started with the second JavaScript course after I finished part 1. I love the video’s and everything is explained very well. It’s a lot of fun.

Could someone help me understand the solution of the section ‘Prototypes’. It’s the exercize where we remodify the Stopwatch(). I found it difficult to understand and it took me some time to make it work only a little bit. I got an error, and after watching the solution I still don’t understand. I copied his code but it doesn’t seem to work eigher:

```
function Stopwatch(){

 let startTime, endTime, running, duration = 0;

  Object.defineProperty(this, 'duration', {

    get: function(){ return duration},

    set: function(value){ duration = value; }

  })

  Object.defineProperty(this, 'startTime', {

    get: function(){ return startTime}

  })

  Object.defineProperty(this, 'endTime', {

    get: function(){ return endTime}

  })

  Object.defineProperty(this, 'running', {

    get: function(){ return running}

  })

} 

Stopwatch.prototype.start = function(){

  if(this.active) 

    throw new Error('Stopwatch has already started');

  this.running = true;

  this.startTime = new Date();

}

Stopwatch.prototype.stop = function(){

  if(!this.running) 

    throw new Error('Stopwatch has already been stopped');

  

  this.running = false;

  

  this.endTime = new Date();

  const seconds = (endTime.getTime() - startTime.getTime()) / 1000;

  this.duration += seconds;

}

Stopwatch.prototype.reset = function(){

  this.startTime = null;

  this.endTime = null;

  this.running = false;

  this.duration = 0;

}

const sw = new Stopwatch();

```

When I type “sw.start()” in the console twice it doesn’t give an error where it suppose to do so, and sw.running is unaffected after (where it suppose to be set to “true”).

Sorry for the long story, here’s my question: What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Cage](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/cage/32/990_2.png) [@Cage](https://forum.codewithmosh.com/u/Cage)\
**Post date:** [February 21, 2021, 4:09pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/2 "2021-02-21T16:09:32Z")

</div>

Ok, I’ve watched the video for the 10th time… It still doesn’t work.

In the end Mosh tells us the duration can’t be set because we need to implement a setter. Shouldn’t this then also apply to “running”? We are also updating this value within the start and stop method.

Please help me 🙂 I’m a sponge desperate for more information 😛

---

<div class="post-metadata">

**Author:** ![William](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@William](https://forum.codewithmosh.com/u/William)\
**Post date:** [February 22, 2021, 1:40pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/3 "2021-02-22T13:40:27Z")

</div>

` if(this.active)`  
Should this not be:  
`if(this.running)`

---

<div class="post-metadata">

**Author:** ![William](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@William](https://forum.codewithmosh.com/u/William)\
**Post date:** [February 22, 2021, 1:45pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/4 "2021-02-22T13:45:26Z")

</div>

I watched the video. Is the solution important in this excercise? Isn’t the conclusion changes to improve the code is sometimes creating problems?

---

<div class="post-metadata">

**Author:** ![Cage](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/cage/32/990_2.png) [@Cage](https://forum.codewithmosh.com/u/Cage)\
**Post date:** [February 22, 2021, 2:03pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/5 "2021-02-22T14:03:58Z")

</div>

You are totally right, I used “active” in my code and forgot to change it.

And I think you are right about the conclusion too. I just wanted to make it work without errors and his version doesn’t work at all.

Thanks a lot for the reply!

---

<div class="post-metadata">

**Author:** ![kurukuru](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/kurukuru/32/2824_2.png) [@kurukuru](https://forum.codewithmosh.com/u/kurukuru)\
**Post date:** [December 19, 2021, 2:35am UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/6 "2021-12-19T02:35:57Z")

</div>

To whom it maybe concern:

- Remove all the declarations in the function.
- Modify the duration to display window.duration. (global)
- Declare ‘duration’ outside the StopWatch() function.
- In reset, remember to set window.duration to 0.
- Not perfect but it’s working 😃.

My solution:

```auto
 function StopWatch() {
  Object.defineProperty(this, 'duration', {
    get: () => window.duration
  });
}

this.duration = 0;

StopWatch.prototype.start = () => {
  if (this.isRunning)
    throw new Error('StopWatch has already started!');
  this.isRunning = true;
  this.timeStart = new Date().getTime();
};

StopWatch.prototype.stop = () => {
  if (!this.isRunning)
    throw new Error('StopWatch has already stopped!');
  this.isRunning = false;
  this.timeStop = new Date().getTime();
  const seconds = (timeStop - timeStart) / 1000;
  this.duration += seconds;
};

StopWatch.prototype.reset = () => {
  this.timeStart, this.timeStop, this.isRunning, window.duration = 0;
};

```

---

<div class="post-metadata">

**Author:** ![harshNaik](https://avatars.discourse-cdn.com/v4/letter/h/41988e/32.png) [@harshNaik](https://forum.codewithmosh.com/u/harshNaik)\
**Post date:** [May 27, 2024, 12:17pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/7 "2024-05-27T12:17:54Z")

</div>

exactly, i was thinking same, did you understood ?

---

<div class="post-metadata">

**Author:** ![wildlux](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/wildlux/32/9056_2.png) [@wildlux](https://forum.codewithmosh.com/u/wildlux)\
**Post date:** [January 29, 2025, 3:22pm UTC](https://forum.codewithmosh.com/t/javascript-pt2-prototypes-solution-doesnt-work/2886/8 "2025-01-29T15:22:04Z")

</div>

onesly i skip without check-it but i stop to prototype.  
In prototype there is a exercize about stopwatch.

First second thirt … till 10 … times i try and it’s not works but i think with the implementation with DOT-prototype it’s will works well.

I am dyslessic and some time i think “it was my problem for dont understand in deeply the topic”, before skip i try too much times. in that context of funzions it’s for me little bit complicete to follow at 100%.

I am also a little bit background of Python and C++.

Me i write on paper first and i re-watch tutorial and write on the file and try what i do in this exercize. It’s only.  
Thanks a lot at all!
