Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function book(arr) {
- let shelf = arr.shift().split("&");
- let commands = arr.shift();
- while (commands !== "Done") {
- let currComamnds = commands.split(" | ");
- let command = currComamnds[0];
- let token = currComamnds[1];
- let token1 = currComamnds[2]
- switch (command) {
- case "Add Book": add(command, token); break;
- case "Take Book": take(command, token); break;
- case "Swap Books": swap(command, token, token1); break;
- case "Insert Book": insert(command, token); break;
- case "Check Book": (command, token); break;
- }
- commands = arr.shift()
- function add(command, token) {
- if (!shelf.includes(token)) {
- shelf.unshift(token);
- }
- }
- function take(command, token) {
- if (shelf.includes(token)) {
- let index = shelf.indexOf(token);
- shelf.splice(index, 1);
- }
- }
- function swap(command, token, token1) {
- if (shelf.includes(token) && shelf.includes(token1)) {
- let index1 = shelf.indexOf(token);
- let index2 = shelf.indexOf(token1);
- let temp = shelf[index1];
- let temp2=shelf[index2]
- shelf[index1] = temp2;
- shelf[index2]=temp
- }
- }
- function insert(command, token) {
- if (!shelf.includes(token)) {
- shelf.push(token);
- }
- }
- function check(command, token) {
- let index = Number(shelf.indexOff(token))
- if (shelf.includes(index)) {
- console.log(shelf[index]);
- }
- }
- }
- console.log(shelf.join(", "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement