Removing a Note: Day Seven of "The Complete Node.js Developer Course"

in #javascript7 years ago

photo5026536570701850556.jpg

A challenge: Understanding the filter function

Thanks to everyone who's been reading and commenting. Especially those who have been commenting with clarifications and answers to my questions.

This app that we are writing is the demo app for the course. It is for learning purposes. Right now, we are writing the JSON data to a file, not to a database.

Previously

In this series: Programming Goals : Day One : Day Two : Day Three : Day Four : Day Five : Day Six

Starting off with the challenge

This section (18. Removing a Note) started off right away with a challenge to write the removeNote function. A key part of writing this was using the filter function to remove (from all the notes) the note with the title passed in as an argument. As some of you know, I was struggling to understand how the filter function worked. Thanks for your explanations. I think I'm getting it better now.

Here's the code I wrote to complete the challenge:

Screenshot from 2017-09-12 00-02-11.png

The line I still struggled with:

var rmDuplicate = notes.filter((note) => note.title !== title)

I played around with this for a while. The challenge took me longer than it should have. I can talk out what this does, but am still having a little trouble with the syntax. I think that the (note) just calls in each of the notes objects from all of the notes and then the next part => note.title !== title) compares the note title (looping through each note) with the title that was passed in as an argument. It keeps the titles that don't match and removes (or filters out) the title that does match. This all gets "saved" in the rmDuplicates variable.

The next line saveNotes(rmDuplicate) saves the filtered version of the notes to the file, overwriting the previous version and as a result removing the note with the title that matched the title passed in through the argument. Hope that makes sense.

Note removal status: removed... or not

The rest of this section covered how to determine if a note was removed or not and to log that out to the screen with a "Note was removed" message or "Note not found" message. It used a "ternary operator", which I have encountered before, but haven't used much.

You can't see all of the code in the project (that would be overkill for a blog post... maybe), but you can probably follow what I'm learning.

Progress

I've been really busy with work and family life this last week and didn't study node.js as much as I should have and I didn't post on Steemit as much as I'd like... but the plan is working. By committing to posting about this, and by having readers and commenters, I am continuing with my learning instead of giving up or "getting too busy".

Please let me know if these posts are helpful. I'm pretty sure that a reader wouldn't be able to learn directly from these posts, but hopefully they'll be an encouragement to someone to take a course or to push through learning something that's not easy.

Thanks for reading!

-- @matthewdavid

Sort:  
Loading...

btw, these are all the same. note is just what you have called the argument and note is a good name as makes it clear what is going on

(note) => note.title !== title
(x) => x.title !== title
(burger)  => burger.title !== title

I have been interested and involved with computers and code since the 1970s as a hobby. I remember the time before hard drives and the A: and B: drives. The Microsoft Disk Operating System (MSDOS) was my first operating system.

I use the Fedora operating system exclusively now. Much more secure than the Windows and Mac systems.

I wish you great fortune with your learning coding. I also wanted to thank you for upvoting my comment on Can I Reconcile Anarchism/Voluntaryism with My Faith? Wrestling with Romans 13.

Have a wonderful and safe week!


BOTTOMBANNERSTEEM.png

Loading...

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57630.83
ETH 3105.65
USDT 1.00
SBD 2.33