Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function library(input) {
- let bookNames = input.shift().split('&');
- let result = '';
- while (input[0] !== "Done") {
- let commands = input.shift().split('|');
- if (commands[0].includes('Add')) {
- if (bookNames.includes(commands[1])) {
- continue;
- }
- bookNames.unshift(commands[1]);
- }
- if (commands[0].includes('Take')) {
- if (bookNames.includes(commands[1])) {
- let indexOfBook = bookNames.indexOf(commands[1]);
- bookNames.splice(indexOfBook, 1);
- }
- }
- if (commands[0].includes('Insert')) {
- bookNames.push(commands[1])
- }
- if (commands[0].includes('Swap')) {
- let firstBook = commands[1]
- let secondBook = commands[2]
- if (bookNames.includes(firstBook) && bookNames.includes(secondBook)) {
- let firstIndex = bookNames.indexOf(firstBook);
- let secondIndex = bookNames.indexOf(secondBook);
- bookNames[firstIndex] = secondBook;
- bookNames[secondIndex] = firstBook;
- }
- }
- }
- console.log(bookNames.join(', '))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement