Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve3 (input=[]) {
- let arr = input.shift().split('&');
- // let commands = input.shift().split(' | ');
- while ( input.length > 0 /*commands[0] !== 'Done'*/) {
- let commands = input.shift().split(' | ');
- let action = commands[0];
- let bookName = commands[1];
- let book2Name = commands[2];
- if (action === 'Add Book') {
- if (arr.includes(bookName)) {
- continue;
- } else {
- arr.unshift(bookName);
- }
- } else if (action === 'Take Book') {
- if (arr.includes(bookName)) {
- let index = arr.indexOf(bookName);
- arr.splice(index, 1);
- }
- } else if (action === 'Swap Books') {
- if (arr.includes(bookName) && arr.includes(book2Name)) {
- let index1 = arr.indexOf(bookName);
- let index2 = arr.indexOf(book2Name);
- let temp = arr[index1];
- arr[index1] = arr[index2];
- arr[index2] = temp;
- }
- } else if (action === 'Insert Book') {
- arr.push(bookName);
- } else if (action === 'Check Book') {
- let index = Number(bookName);
- if ( 0 < index && index < arr.length) {
- console.log(arr[index]);
- }
- }
- // commands = input.shift().split(' | ');
- }
- console.log(arr.join(', '));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement