Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function library(arr) {
- let library = arr.shift().split("&");
- let indexOfBook;
- let indexOfSecondBook;
- for (let token of arr) {
- if (token === "Done" || "") {
- // do nothing
- }
- token = token.split(" | ");
- let action = token.shift();
- let book = token.shift();
- if (action === "Add Book" && !check(book)) {
- library.unshift(book)
- } else if (action === "Take Book" && check(book)) {
- let indexOfBook = library.indexOf(book);
- library.splice(indexOfBook, 1);
- } else if (action === "Swap Books" && check(book) && check(token[0])) {
- let secondBook = token.shift();
- indexOfBook = library.indexOf(book);
- indexOfSecondBook = library.indexOf(secondBook);
- library.splice(indexOfBook, 1, secondBook);
- library.splice(indexOfSecondBook, 1, book);
- } else if (action === "Insert Book" && !check(book)) {
- library.push(book);
- } else if (action === "Check Book" && 0 <= book && book < library.length) {
- console.log(library[book]);
- }
- }
- return library.join(", ")
- function check(checkedItem) {
- if (library.includes(checkedItem)) {
- return true;
- } else {
- return false;
- }
- }
- }
- // console.log(library([
- // 'Don Quixote&The Great Gatsby&Moby Dick&Hamlet',
- // 'Add Book | The Odyssey',
- // 'Take Book | Don Quixote',
- // "Insert Book | Alice's Adventures in Wonderland",
- // 'Check Book | 3',
- // 'Done',
- // '',
- // '',
- // ''
- // ]));
- // console.log(library([
- // 'Anna Karenina&Heart of Darkness&Catch-22& The Stranger',
- // 'Add Book | David Copperfield',
- // 'Add Book | One Thousand and One Nights',
- // 'Swap Books | One Thousand and One Nights | Catch-22',
- // 'Take Book | David Copperfield',
- // 'Insert Book | The Stories of Anton Chekhov',
- // 'Check Book | 17',
- // 'Done',
- // ''
- // ]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement