Need help returning an array with two arrays: borrowed books and returned books

I am trying to return an array with two arrays the first being borrowed books and the other being returned books. The prompt that I have to use is:

The partitionBooksByBorrowedStatus() function in public/src/books.js has a single parameter:

  • An array of books.

It returns an array with two arrays inside of it. All of the inputted books are present in either the first or second array.

The first array contains books that have been loaned out, and are not yet returned while the second array contains books that have been returned. You can check for the return status by looking at the first transaction in the borrows array.

Here is a portion of the data provided:

const books = [{
id: "5f447132d487bd81da01e25e",
title: "sit eiusmod occaecat eu magna",
genre: "Science",
authorId: 8,
borrows: [
  {
    id: "5f446f2e2cfa3e1d234679b9",
    returned: false,
  },
  {
    id: "5f446f2ed3609b719568a415",
    returned: true,
  },
  {
    id: "5f446f2e1c71888e2233621e",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2ede05a0b1e3394d8b",
    returned: true,
  },
  {
    id: "5f446f2e4081699cdc6a2735",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2e6059326d9feb9a68",
    returned: true,
  },
  {
    id: "5f446f2e409f8883af2955dd",
    returned: true,
  },
  {
    id: "5f446f2e3900dfec59489477",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2ef2ab5f5a9f60c4f2",
    returned: true,
  },
  {
    id: "5f446f2ea6b68cf6f85f6e28",
    returned: true,
  },
  {
    id: "5f446f2eed18105706d6ca19",
    returned: true,
  },
  {
    id: "5f446f2eae901a82e0259947",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2e5aa2bb5545a0f8a6",
    returned: true,
  },
  {
    id: "5f446f2ea508b6a99c3e42c6",
    returned: true,
  },
  {
    id: "5f446f2e50cc2da9cd80efdb",
    returned: true,
  },
  {
    id: "5f446f2e0b3e2ff72fc503e7",
    returned: true,
  },
  {
    id: "5f446f2e91c2af00cb74e82b",
    returned: true,
  },
  {
    id: "5f446f2ef795e593cd3cd19d",
    returned: true,
  },
  {
    id: "5f446f2e2f35653fa80bf490",
    returned: true,
  },

Here is what I have tried so far:

function partitionBooksByBorrowedStatus(books) {

  let available = [];
  let unavailable = [];
  const bookStatuses = [];

  books.forEach((book) => {
   const isBookReturned = book.borrows[0].returned;
   if (isBookReturned) { // if book is returned
     available.push(book);
   } else { // if book is not returned
     unavailable.push(book);
   }
 });

 bookStatuses.push(available);
 bookStatuses.push(unavailable);

 return bookStatuses; //

}

When I run the code it returns an error that states "Expected 3 to equal 6" for the life of me I cannot figure it out. Your comments and suggestions would be much appreciated, thanks!



Read more here: https://stackoverflow.com/questions/68462964/need-help-returning-an-array-with-two-arrays-borrowed-books-and-returned-books

Content Attribution

This content was originally published by SwishitySwoosh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: