Build an app that is functionally similar to this example project. Try not to copy the example project, give it your own personal style.
In this lab, you will build a freeCodeCamp forum leaderboard that displays the latest topics, users, and replies from the freeCodeCamp forum. The HTML, CSS and part of the JS have been provided for you. Feel free to explore them.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
- You should have a function named
timeAgothat takes a timestamp in the ISO 8601 format as the argument. - The
timeAgofunction should compute the time difference between the time passed as an argument and the current time and return:xm ago(xrepresents minutes) if the amount of minutes that have passed is less than60.xh ago(xrepresents hours) if the amount of hours that have passed is less than24.xd ago(xrepresents days) otherwise.
- You should have a function named
viewCountthat takes the number of views of a post as the argument. - If the value of the views passed as the argument is greater than or equal to
1000, theviewCountfunction should return a string with the views value divided by1000, rounded down to the nearest whole number and the letterkappended to it. Otherwise, it should return the views value. - You should have a function named
forumCategorythat takes the id of a selected category as the argument. - The
forumCategoryfunction should verify that the selected category id is a property of theallCategoriesobject and should return a string containing an anchor element with:- the text of the
categorykey of the selected category. - a class of
categoryfollowed by theclassNameproperty of the selected category. - an
hrefwith the value of<forumCategoryUrl>/<className>/<id>, where<className>is theclassNameproperty of the selected category andidis the argument passed toforumCategory.
- the text of the
- If the
allCategoriesobject does not have the selected category id as its property,categoryshould be indicated asGeneralandclassNameshould be indicated asgeneral. - You should have a function named
avatarsthat takes two arrays representing posters and users, respectively. - The
avatarsfunction should return a string made by joiningimgelements, one for eachuser_idin thepostersarray. Find theimgURL by looking up theuser_idproperty in thepostersarray and find the matchingidproperty in theusersarray. - The
avatarsfunction should set each avatar's size by accessing theavatar_templateproperty and replacing{size}with30. - Each image element should have an alt text with the value of the
nameproperty of the poster. - Each image element should have a source with the value of the
avatar_templateproperty of the poster. In caseavatar_templatecontains a relative path, you should set the source to<avatarUrl>/<avatar_template>. - You should have a function named
showLatestPoststhat takes a single parameter. - The
showLatestPostsshould extract theusersandtopic_listproperties from the object passed as argument. Also, it should process the following properties of the objects from thetopicsarray, which is contained intopic_list:id: the id of the posttitle: the title of the postviews: the number of views of the postposts_count: the number of replies to the topicslug: the slug of the postposters: the posters for that topiccategory_id: an integer indicating the category id for the postbumped_at: a timestamp in the ISO 8601 format
- The
showLatestPostsshould set the inner HTML of#posts-containerto a string made by joiningtrelements, one for each item intopics. - Each
trelement should have fivetdelements in it:- a
tdcontaining two anchor elements, one with the class ofpost-title, anhrefof<forumTopicUrl><slug>/<id>, an anchor text of<title>, and one obtained by callingforumCategorywithcategory_id. - a
tdcontaining adivelement with classavatar-containerthat contains the images returned by theavatarsfunction called withpostersandusersas arguments. - a
tdcontaining the number of replies to the post. Hint: useposts_count - 1. - a
tdcontaining the number of views of the post. - a
tdcontaining the time passed since the last activity.
- a
- You should have an async function named
fetchData. - The
fetchDatafunction should request data fromforumLatestand callshowLatestPostspassing it the response parsed as JSON. - If there's an error when fetching data, the
fetchDatafunction should log the error to the console. You should specifically useconsole.logfor this.
- You should have a function named
timeAgothat takes a single argument. - When the time difference between the time passed as argument and the current time is
50minutes,timeAgoshould return50m ago. - When the time difference between the time passed as argument and the current time is
60minutes,timeAgoshould return1h ago. - When the time difference between the time passed as argument and the current time is
115minutes,timeAgoshould return1h ago. - When the time difference between the time passed as argument and the current time is
15hours,timeAgoshould return15h ago. - When the time difference between the time passed as argument and the current time is
24hours,timeAgoshould return1d ago. - When the time difference between the time passed as argument and the current time is
46hours,timeAgoshould return1d ago. - When the time difference between the time passed as argument and the current time is
3days,timeAgoshould return3d ago. - You should have a function named
viewCountthat takes a single argument. viewCount(597)should return597.viewCount(1000)should return1k.viewCount(2730)should return2k.- You should have a function named
forumCategorythat takes a single argument. forumCategory(299)should return a string containing an anchor element with the textCareer Advice.forumCategory(299)should return a string containing an anchor element withhref="https://forum.freecodecamp.org/c/career/299".forumCategory(299)should return a string containing an anchor element withclass="category career".forumCategory(200)should return a string containing an anchor element with the textGeneral.forumCategory(200)should return a string containing an anchor element withhref="https://forum.freecodecamp.org/c/general/200".forumCategory(200)should return a string containing an anchor element withclass="category general".- You should have a function named
avatarsthat takes two arguments. - The
avatarsfunction should return a string made by joiningimgelements, one for each poster found in the user array. - Each
imgelement in the string returned by theavatarsfunction should have analttext with the value of thenameproperty of the poster. - The
avatarsfunction should set each avatar's size by accessing theavatar_templateproperty and replacing{size}with30. - Each
imgelement in the string returned by theavatarsfunction should have thesrcwith the value of theavatar_templateproperty of the poster. In caseavatar_templatecontains a relative path, it should be set to<avatarUrl>/<avatar_template>. - You should have a function named
showLatestPoststhat takes a single parameter. - You should have a function named
fetchData. - Your
fetchDatafunction should request data fromhttps://cdn.freecodecamp.org/curriculum/forum-latest/latest.json. - Your
fetchDatafunction should callshowLatestPosts. - If there is an error, your
fetchDatafunction should log the error to the console, usingconsole.log. showLatestPostsshould set the inner HTML of#posts-containerto a string made by joiningtrelements, one for each item intopics.- Each
trelement from the string returned byshowLatestPostsshould contain 5tdelements. - The first
tdelement of each table row from the string returned byshowLatestPostsshould contain two anchor elements, the first with the class ofpost-title, anhrefof<forumTopicUrl><slug>/<id>, an anchor text of<title>, and the second obtained by callingforumCategorywithcategory_id. - The second
tdelement of each table row from the string returned byshowLatestPostsshould contain the images returned by theavatarsfunction called withpostersandusersas arguments, nested within adivelement with the class ofavatar-container. - The third
tdelement of each table row from the string returned byshowLatestPostsshould contain the number of replies to the post. Hint: useposts_count - 1. - The fourth
tdelement of each table row from the string returned byshowLatestPostsshould contain the number of views of the post. - The fifth
tdelement of each table row from the string returned byshowLatestPostsshould contain time passed since the last activity, generated using thetimeAgofunction.