codingforeveryone/js

New member #5 - Article Archive

Closed this issue · 8 comments

You are a first-time online publisher and would like to post your articles online. You want to build an archive which can store and update article information and enables "likes" from social networks.

The "archive" array will store an object for each article. Each object shall contain the following article info:

  • "articleId" - each article has its id. The 1st article has id 1, the 2nd id 2 and so on. The value must be an integer.
  • "title" - any string value.
  • "filename" - this value will store the filename where the article is stored, for example, 'path.txt'. The file should always have the ".txt" extension. The filename will use the provided article "title" (previous field). Blank spaces in the title shall be replaced with "_", while the following characters shall be discarded in the filename: (){}[]/.,;!?`+@%^#$£
  • "likes" - shows how many people have liked the article on each of the social networks in the "networks" array.

Please use the provided names for each info-field.

You should write the following functions:

  • "article" - a constructor which takes the "title" argument to create an article object with the above listed info, and add it to the archive. For example:
new article("Something, Something!");
//would return:
{
  articleId: 1,
  title:"Something, Something!",
  filename: "Something_Something.txt",
  likes: {facebook:0,twitter:0,googlePlus:0}
  }
  • "like" - a function which takes the "title" and "network" arguments, and "+1"s the total social-netowork likes of that article. For example:
like("Something, Something!","twitter");
//would return:
{facebook:0,twitter:1,googlePlus:0}
  • "remove" - a method which takes the "title" argument and removes the the article with such title from the archive. The other articles' id's will be updated accordingly. For example, let's say we added a 2nd article named "Fancy new article" to our archive, and decided later to remove the previous article:
remove("Something!, Something!");
//would return:
[{
  articleId: 1,
  title:"Fancy new article",
  filename: "Fancy_new_article.txt",
  likes: {facebook:0,twitter:0,googlePlus:0}
  }]

i intend to solve this problem

I have already solved this problem. I'm not going to push it because @abusabir showed his intention to solve it before. But I put here the link to my solution. I have made some improvements like, for example, the archive is now an object instead of an array, it has methods to add articles and other functions which were supposed to be global functions, but I think this is a better approach.

@gabrielperales I like your approach. At the time of the issue creation I probably wasn't comfortable enough with function-calls from objects. It's not so long ago, but I've learned a lot since then!
In any case the new member problems are designed to be beginner-friendly so that the members get comfortable using github. good job!

@abusabir what is your status on the problem-solving?

@franzmoro one weird thing I've seen in your description about the issue, is that when you remove an article from the archive... article's Id from the archive are recalculated. I think this is not the best design (you shouldn't change the reference to those articles). My first approach was this. There, the constructor function of Article is envolved in a closure where there is a variable which works as a counter, that variable is used to assign the Id to the new articles.

I hope I have explained it well...

@gabrielperales you have explained it well and, as I said, I like your approach.
However, the exercise is designed to be simple so that beginners can have a play with js and github. For this reason I don't see the need to re-write the exercise. If we were instead aiming at making a proper article archive we would re-think the code, and your approach would be better than what I wrote in the exercise.

Yes, I know 👍 . I have had a lot of fun solving your issue, it is very well explained. Good job too ;-)