Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(array) {
- let copy = array.slice();
- let inventory = copy[0].split(', ')
- for (let i = 1; i < array.length; i++) {
- if (array[i].includes(' - ')) {
- let input = array[i].split(' - ');
- let [command, ...intem] = input;
- let current = intem[0];
- if (command === 'Collect') {
- if (!inventory.includes(current)) {
- inventory.push(current)
- }
- }else if (command === 'Drop'){
- if (inventory.includes(current)){
- inventory.splice(inventory.indexOf(current), 1)
- }
- }else if(command === 'Combine Items'){
- let [oldItem, newItem] = current.split(':');
- if (inventory.includes(oldItem)){
- let indexToPlace = inventory.indexOf(oldItem);
- inventory.splice(indexToPlace+1, 0, newItem)
- }
- }else if(command === 'Renew'){
- if (inventory.includes(current)){
- let indexToPlace = inventory.indexOf(current);
- inventory.splice(inventory.indexOf(current), 1)
- inventory.push(current);
- }
- }
- } else if (array[i] === 'Craft!') {
- console.log(inventory.join(', '));
- }
- }
- }
- solve(['Iron, Wood, Sword', 'Collect - Gold', 'Drop - Wood', 'Craft!'])
- solve(['Iron, Sword', 'Drop - Bronze', 'Combine Items - Sword:Bow', 'Renew - Iron', 'Craft!'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement