Last active
May 11, 2021 15:13
-
-
Save borislavstoychev/3ec3760d2bbff76f0c114258ea835377 to your computer and use it in GitHub Desktop.
error lest test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gladiatorInventory(arr) { | |
let inventory = arr.shift().split(" "); | |
const actions = { | |
Buy: (equipment) => buy(equipment), | |
Trash: (equipment) => trash(equipment), | |
Repair: (equipment) => repair(equipment), | |
Upgrade: (param) => upgrade(param) | |
}; | |
function buy(eq) { | |
inventory.push(eq); | |
} | |
function trash(eq) { | |
if (inventory.includes(eq)){ | |
inventory.filter(x => x !== eq); | |
} | |
} | |
function repair(eq) { | |
if (inventory.includes(eq)) { | |
inventory.filter(x => x !== eq); | |
inventory.push(eq); | |
} | |
} | |
function upgrade(param) { | |
let [eq, up] = param.split("-") | |
if (inventory.includes(eq)) { | |
inventory.splice(inventory.indexOf(eq) + 1, 0, `${eq}:${up}`); | |
} | |
} | |
arr.forEach(element => { | |
const [action, param1, ...param2] = element.split(' '); | |
actions[action](param1, param2); | |
}); | |
console.log(inventory.join(" ")); | |
} | |
// gladiatorInventory(['SWORD Shield Spear', | |
// 'Buy Bag', | |
// 'Trash Shield', | |
// 'Repair Spear', | |
// 'Upgrade SWORD-Steel']); | |
gladiatorInventory(['SWORD Shield Spear', | |
'Trash Bow', | |
'Repair Shield', | |
'Upgrade Helmet-V'] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment