Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function problem2(array) {
- let biscuits = array.shift().split(', ');
- let command = array.shift();
- while (command != 'No More Money') {
- let tokens = command.split(' ');
- let action = tokens[0];
- switch (action) {
- case 'OutOfStock':
- let outOfStockBiscuit = tokens[1];
- for (let i = 0; i < biscuits.length; i++) {
- let biscuit = biscuits[i];
- if (biscuit == outOfStockBiscuit) {
- biscuits.splice(i, 1, 'None')
- }
- }
- break;
- case 'Required':
- let requiredBiscuit = tokens[1];
- let index = Number(tokens[2]);
- if (index >= 0 && index < biscuits.length) {
- if (biscuits[index] != 'None') {
- biscuits.splice(index, 1, requiredBiscuit);
- }
- }
- break;
- case 'Last':
- let pushBiscuit = tokens[1];
- biscuits.push(pushBiscuit);
- break;
- }
- command = array.shift();
- }
- let biscuitsForPrint = biscuits.filter(x => x != 'None');
- console.log(biscuitsForPrint.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment