Node.js-Design-Patterns-Third-Edition icon indicating copy to clipboard operation
Node.js-Design-Patterns-Third-Edition copied to clipboard

Are there answers for the exercises

Open stuartambient opened this issue 4 years ago • 19 comments

Haven't seen them on packt or here, maybe they don't exist ?

stuartambient avatar Nov 13 '20 10:11 stuartambient

Hey @stuartambient, thanks a lot for opening this issue. As of today, we don't have yet a place to collect solutions for the various coding tests.

I think GitHub might be a great place to start collecting some. I just created this wiki page to do that: https://github.com/PacktPublishing/Node.js-Design-Patterns-Third-Edition/wiki/Node.js-Design-Patterns-Third-Edition---Exercise-Solutions

Have you already implemented any solution for these exercises? 😉

lmammino avatar Nov 13 '20 17:11 lmammino

Thanks for putting that page up, I'm subscribed and will certainly share my solutions. I just finished reading Chapter 3 and did 3.1 (a minor tweak of a challenge) but 3.2 confused me a bit regarding the number that gets sent to the function. I couldn't tell if it was a number in milliseconds or the number of times setTimeout should run.

Typically I prefer to do a challenge first and then look over others solutions afterward (if they are available). Thanks for the response and creating what is in my opinion an invaluable and comprehensive Node book.

stuartambient avatar Nov 13 '20 17:11 stuartambient

Is this the right solution for 3.1? Or should we clean up the event listener after the start event is emitted in case someone uses .on for this event instead of .once and the file that is processed is so enormous that it takes doesnt make sense to just wait till the instance gets garbage collected. https://github.com/enricoschaaf/node-js-design-patterns-exercises/blob/main/exercises/3.1/index.js

enricoschaaf avatar Nov 15 '20 16:11 enricoschaaf

I just added a line to the for loop in add()

find() {
    for (const file of this.files) {
      **this.emit('starting', `starting regex search on file: ${file}`);**
      readFile(file, 'utf8', (err, content) => {
........
}
Then in call
.addFile('fileA.txt')
  .addFile('fileB.json')
  **.on('starting', message => console.log(`find start message: ${message}`))**
  .find()

Probably would be a good idea for me to add a remove listener at the end.

stuartambient avatar Nov 15 '20 18:11 stuartambient

@enricoschaaf that looks like the right solution. I wouldn't overthink too much the cleaning part unless it would cause a memory leak, let's leave Node.js do its job. Do you want to add your solution to https://github.com/PacktPublishing/Node.js-Design-Patterns-Third-Edition/wiki/Node.js-Design-Patterns-Third-Edition---Exercises-Solutions?

@stuartambient beware of Zalgo when emitting the "starting" event!

mariocasciaro avatar Nov 15 '20 21:11 mariocasciaro

Sure, is there a way to provide pull requests to wiki's? Could not find any.

enricoschaaf avatar Nov 15 '20 22:11 enricoschaaf

I'll throw 3.2 out. https://gist.github.com/stuartambient/7485f29ace060eb6600ca61195d590a6

stuartambient avatar Nov 15 '20 23:11 stuartambient

@enricoschaaf, @stuartambient I think I finally managed to configure the wiki correctly. Can you tell me if you can see the "edit" button now?

Thanks

lmammino avatar Nov 16 '20 08:11 lmammino

@enricoschaaf, @stuartambient I think I finally managed to configure the wiki correctly. Can you tell me if you can see the "edit" button now?

The link went straight to a new page form. I added some stubs so the home page has the stub pages in the right panel. Buttons for edit / new are there as well.

Problem though is the link above is now just a page in the TOC. It didn't keep it as the wiki home page. I think maybe only the repo owner would be able to change it.

stuartambient avatar Nov 16 '20 14:11 stuartambient

@enricoschaaf, @stuartambient I think I finally managed to configure the wiki correctly. Can you tell me if you can see the "edit" button now?

Thanks

Works👍

enricoschaaf avatar Nov 16 '20 14:11 enricoschaaf

updated my 3.2 if it's helpful - https://gist.github.com/stuartambient/7485f29ace060eb6600ca61195d590a6

stuartambient avatar Nov 17 '20 01:11 stuartambient

@stuartambient, I added your solution to the list :)

lmammino avatar Nov 17 '20 18:11 lmammino

Solution 3.4 https://gist.github.com/stuartambient/d7f4c16fb7af21b2ae8197bfabcb7ac1 This is the right place to suggest a solution, or no ?

stuartambient avatar Nov 18 '20 02:11 stuartambient

I am copying them to this wiki page: https://github.com/PacktPublishing/Node.js-Design-Patterns-Third-Edition/wiki/Node.js-Design-Patterns-Third-Edition---Exercise-Solutions

You should be able to edit it if you want to add more in the future :)

lmammino avatar Nov 18 '20 08:11 lmammino

Hey @stuartambient and @enricoschaaf, looks like we're all at about the same point in the book. Would either of you be interested in starting a Gitter or something like that to chat about what we're learning and help each other figuring out solutions to the EOC problems?

Jack-Barry avatar Dec 18 '20 23:12 Jack-Barry

Hey @Jack-Barry, @stuartambient & @enricoschaaf. I love the idea of offering a place for readers to discuss. I just enabled "GitHub discussions" on this repo (https://github.com/PacktPublishing/Node.js-Design-Patterns-Third-Edition/discussions). Do you think this can help you or do you prefer something like a chat (slack or discord style)?

lmammino avatar Dec 19 '20 10:12 lmammino

@lmammino That should work, I didn't even know about that feature in GitHub. That should make it easier for anyone using this resource to participate without having to have an account for another service.

Thanks for setting it up!

Jack-Barry avatar Dec 19 '20 13:12 Jack-Barry

@Jack-Barry thanks for reaching out. I also agree that the discussions here would work fine. Personally I've been working slowly through the book. I know that this book has come up in a few recent r/node threads and got positive comments and upvotes.

stuartambient avatar Dec 20 '20 17:12 stuartambient

@stuartambient Thanks for the heads up, I'll keep an eye out in r/node. I've started a discussion thread for Chapters 4 & 5 here. I'm reading through Chapter 6 but haven't completed exercise 5.3 or 5.4 yet. 5.3 seems easy enough, 5.4 looks a little more involved so I might not get through it until after the holidays

Jack-Barry avatar Dec 20 '20 22:12 Jack-Barry