Array.protoype.slice not working

I am working on a project which displays random articles. I am using JSONPLACEHOLDER for the title and body and RANDOMUSER for creating each article’s author.

All this data is then used to create an array articles containing several article objects.
To render these articles in the HTML, displayCards() is invoked.
The function gets the no of articles previously situated in the DOM and then add 10 more articles using the template element.

The problem is articles.list.slice(n , n + 10) is returning an empty array but articles.list contain 100 entries. I am stuck.

Link to the codepen

It’s not a problem with .slice, but rather where/when you invoke the displayCards() function. The async function that fetches the articles isn’t done yet, but displayCards() runs immediately, so the articles array is still empty. A possible fix is to move displayCards() into the async function.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.