Treasure hunt
Защо judge ми дава 80/100 ?
https://judge.softuni.org/Contests/Practice/Index/1773#1
function treasure(arr) {
let chest = arr.shift().split("|");
let command = arr.shift();
let steal = [];
let counter = 0;
while (command != "Yohoho!") {
let tokens = command.split(" ");
let action = tokens.shift();
if (action == "Loot") {
let treasureToLoot = tokens.slice(0);
for (let treasure of treasureToLoot) {
if (!chest.includes(treasure)) {
chest.unshift(treasure);
}
}
} else if (action == "Drop") {
let idx = Number(tokens[0]);
if (idx >= 0 && idx < chest.length) {
let theLoot = chest.splice(idx, 1);
chest.push(theLoot);
}
} else if (action == "Steal") {
let count = Number(tokens[0]);
if (count > chest.length + 1) {
steal = chest;
console.log(steal);
} else {
steal = chest.splice(-count,);
}
}
console.log(steal.join(", "));
command = arr.shift();
}
if (chest.length == 0) {
console.log("Failed treasure hunt.");
} else {
for (let el of chest) {
for (let i = 0; i < el.length; i++) {
counter++;
}
}
console.log(`Average treasure gain: ${(counter / chest.length).toFixed(2)} pirate credits.`)
}
}
treasure([
"Gold|Silver|Bronze|Medallion|Cup",
"Loot Wood Gold Coins",
"Loot Silver Pistol",
"Drop 3",
"Steal 3",
"Yohoho!",
])
Благодаря!