Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let inventory = input.shift().split(' ');
- let commands = input;
- for (let i=0; i<commands.length;i++) {
- let tokens = commands[i].split(' ');
- if (tokens[0]==='Buy') {
- if (!inventory.includes(tokens[1])) {
- inventory.push(tokens[1]);
- }
- } else if (tokens[0]==='Trash') {
- if (inventory.includes(tokens[1])) {
- inventory.splice(inventory.indexOf(tokens[1]), 1);
- }
- } else if (tokens[0]==='Repair') {
- if (inventory.includes(tokens[1])) {
- inventory.splice(inventory.indexOf(tokens[1]), 1);
- inventory.push(tokens[1]);
- }
- } else if (tokens[0]==='Upgrade') {
- let upgradeArray = tokens[1].split('-');
- let el=upgradeArray[0];
- if (inventory.includes(el)) {
- inventory.splice(inventory.indexOf(el)+1, 0, el+':'+upgradeArray[1]);
- }
- }
- }
- console.log(inventory.join(' '));
- }
- /*solve(['SWORD Shield Spear',
- 'Buy Bag',
- 'Trash Shield',
- 'Repair Spear',
- 'Upgrade SWORD-Steel']
- );*/
Advertisement
Add Comment
Please, Sign In to add comment