Skip to content

Instantly share code, notes, and snippets.

@stoneboyindc
Last active August 6, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stoneboyindc/417b7cfdf8685b4d1ddef5dee6f44f78 to your computer and use it in GitHub Desktop.
Save stoneboyindc/417b7cfdf8685b4d1ddef5dee6f44f78 to your computer and use it in GitHub Desktop.
getBooksPossessedByAccount()
function getBooksPossessedByAccount(account, books, authors) {
//Initialze a return array
let booksPossessed=[];
//check for the account id in the borrows arrays
books.forEach(book => {
let borrowArray = book.borrows;
if (borrowArray.find(borrow => borrow.id === account.id && borrow.returned === false)) {
booksPossessed.push(book);
}
})
booksPossessed.forEach(book=>{
let author = authors.find(person => person.id === book.authorId);
book['author'] = author;
})
console.log(booksPossessed);
return booksPossessed;
}
@stoneboyindc
Copy link
Author

stoneboyindc commented Aug 6, 2021

function getBooksPossessedByAccount(account, books, authors) {
  let accountID = account.id;
  let borrowed = [];
  for (let i = 0; i < books.length; i++)
  {
    for (let j = 0; j< books[i].borrows.length; j++)
    {
      if (books[i].borrows[j].id == account.id && books[i].borrows[j].returned == false){
        borrowed.push(books[i]);
      }
    }
  }
  return borrowed;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment