Advertisement
VDimov01

SchoolLibrary

Oct 26th, 2021
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. function library(input){
  2. let bookshelf = input.shift().split('&')
  3.  
  4. for(let element of input){
  5. let [activity, book1OrIndex, book2] = element.split(' | ')
  6. let [command, useless] = activity.split(' ')
  7. let checkBook1 = bookshelf.includes(book1OrIndex)
  8. let checkbook2 = bookshelf.includes(book2)
  9.  
  10. if(command === 'Add'){
  11. if(!checkBook1){
  12. bookshelf.unshift(book1OrIndex)
  13. }else{ continue }
  14. }
  15.  
  16. if(command === 'Take'){
  17. if(checkBook1){
  18. let index = bookshelf.indexOf(book1OrIndex)
  19. bookshelf.splice(index, 1)
  20. }else{ continue }
  21. }
  22.  
  23. if(command === 'Swap'){
  24. if(checkBook1 && checkbook2){
  25. let index1 = bookshelf.indexOf(book1OrIndex)
  26. let index2 = bookshelf.indexOf(book2)
  27.  
  28. let s = bookshelf[index1]
  29. bookshelf[index1] = bookshelf[index2]
  30. bookshelf[index2] = s
  31. }else{ continue }
  32. }
  33.  
  34. if(command === 'Insert'){
  35. if(!checkBook1){
  36. bookshelf.push(book1OrIndex)
  37. }else{ continue }
  38. }
  39.  
  40. if(command === 'Check'){
  41. book1OrIndex = Number(book1OrIndex)
  42. let bookshelfL = bookshelf.length
  43.  
  44. if(book1OrIndex >= 0 && book1OrIndex < bookshelfL){
  45. console.log(bookshelf[book1OrIndex])
  46. }else{ continue }
  47. }
  48.  
  49. if(command === 'Done'){
  50. console.log(bookshelf.join(', '))
  51. break;
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement