Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let gifts = input.shift().split(' ');
- for (let line of input) {
- if (line === 'No Money') {
- break;
- }
- let tokens = line.split(' ');
- let command = tokens[0];
- let gift = tokens[1];
- if (command === 'OutOfStock') {
- if (gifts.indexOf(gift) !== -1) {
- for (let i = 0; i < gifts.length; i++) {
- if (gifts[i] === gift) {
- gifts[i] = 'None';
- }
- }
- }
- } else if (command === 'Required') {
- let index = Number(tokens[2]);
- if (index >= 0 && index < gifts.length) {
- gifts[index] = gift;
- }
- } else if (command === 'JustInCase') {
- gifts[gifts.length - 1] = gift;
- }
- }
- gifts = gifts.filter((gift) => {
- return gift !== 'None';
- });
- console.log(gifts.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement