Advertisement
Lulunga

mid exam 02. Easter Gifts

Jun 29th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. function solve(input) {
  2. let gifts = input.shift().split(' ');
  3.  
  4. for (let line of input) {
  5. if (line === 'No Money') {
  6. break;
  7. }
  8. let tokens = line.split(' ');
  9. let command = tokens[0];
  10. let gift = tokens[1];
  11.  
  12. if (command === 'OutOfStock') {
  13.  
  14. if (gifts.indexOf(gift) !== -1) {
  15. for (let i = 0; i < gifts.length; i++) {
  16. if (gifts[i] === gift) {
  17. gifts[i] = 'None';
  18. }
  19. }
  20.  
  21. }
  22. } else if (command === 'Required') {
  23.  
  24. let index = Number(tokens[2]);
  25. if (index >= 0 && index < gifts.length) {
  26. gifts[index] = gift;
  27. }
  28. } else if (command === 'JustInCase') {
  29. gifts[gifts.length - 1] = gift;
  30. }
  31. }
  32. gifts = gifts.filter((gift) => {
  33. return gift !== 'None';
  34. });
  35. console.log(gifts.join(' '));
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement