Advertisement
Guest User

Easter Gifts - JS

a guest
Jun 28th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. function easterGifts(input) {
  2. let plannedGifts = input.shift().split(" ");
  3.  
  4. for (let i = 0; i < input.length; i++) {
  5. let currentNote = input[i].split(' ');
  6. let command = currentNote[0];
  7.  
  8. switch (command) {
  9. case 'OutOfStock':
  10. let selectedGift = currentNote[1];
  11. if (plannedGifts.includes(selectedGift)) {
  12. for (let j = 0; j < plannedGifts.length; j++) {
  13. plannedGifts[plannedGifts.indexOf(selectedGift)] = "None";
  14. }
  15. }
  16. break;
  17. case 'Required':
  18. let requiredGift = currentNote[1];
  19. let requiredIndex = +currentNote[2];
  20. if (requiredIndex >= 0 && requiredIndex <= plannedGifts.length) {
  21. plannedGifts[requiredIndex] = requiredGift;
  22. }
  23. break;
  24. case 'JustInCase':
  25. let inCaseGift = currentNote[1];
  26. plannedGifts.pop();
  27. plannedGifts.push(inCaseGift);
  28. break;
  29. case 'No money':
  30. break;
  31. }
  32. }
  33. for (let singleGift of plannedGifts) {
  34. if (singleGift === 'None') {
  35. let giftIndex = plannedGifts.indexOf(singleGift);
  36. if (giftIndex < plannedGifts.length) {
  37. plannedGifts.splice(giftIndex, 1);
  38. }
  39. }
  40. }
  41. console.log(plannedGifts.join(' '));
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement