Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let bookShelves = input.shift().split("&");
- for (book of input) {
- let [currentCommand, currentBook, currentBook2] = book.split(" | ");
- if (currentCommand === 'Add Book') {
- if (!bookShelves.includes(currentBook)) {
- bookShelves.unshift(currentBook);
- }
- } else if (currentCommand === 'Take Book') {
- bookShelves = bookShelves.filter(book => book !== currentBook);
- } else if (currentCommand ==='Insert Book') {
- bookShelves.push(currentBook);
- } else if (currentCommand === 'Check Book') {
- let index = Number(currentBook);
- if(bookShelves[index] !== undefined) {
- console.log(bookShelves[index]);
- }
- } else if(currentCommand === 'Swap Books') {
- let [idx1, idx2] = [bookShelves.indexOf(currentBook), bookShelves.indexOf(currentBook2)];
- if(idx1 !== -1 && idx2 !== -1) {
- [bookShelves[idx1], bookShelves[idx2]] = [currentBook2, currentBook];
- }
- } else if (currentCommand === 'Done') {
- console.log(bookShelves.join(', '));
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement