Treasure Hunt
70/100
Не можах да открия грешката
https://pastebin.com/pBty3Wdv
70/100
Не можах да открия грешката
https://pastebin.com/pBty3Wdv
Your code seems to be OK, but check out the below JS solution in order to see a working solution and try to implement a dfferent approach. As svephoto has noted in a noted in a previous post with JAVA => https://softuni.bg/forum/41272/treasure-hunt-ot-mid-exam-retake-6-august-2019, the problem most likely is located in the Steal function, which must be set up in a particular way in order to work:
function tresureHunt(array) {
let initialTresure = array.shift().split('|');
let total = 0;
let isIndexValid = (index, arr) => index >= 0 && index < arr.length;
for (const line of array) {
let [command, ...elements] = line.split(' ');
if (command === 'Yohoho!') {
break;
}
if (command === 'Loot') {
for (const item of elements) {
if (!initialTresure.includes(item)) {
initialTresure.unshift(item);
}
}
} else if (command === 'Drop') {
let index = Number(elements[0]);
if (isIndexValid(index, initialTresure)) {
let dropped = initialTresure.splice(index, 1);
initialTresure.push(...dropped);
}
} else if (command === 'Steal') {
let index = Number(elements[0]);
let stealedTresure = initialTresure.slice(-index);
initialTresure.splice(-index);
console.log(stealedTresure.join(', '));
}
}
total = initialTresure
.reduce((sum, initialTresure) => sum + initialTresure.length, 0) / initialTresure.length;
if (initialTresure.length > 0) {
return `Average treasure gain: ${total.toFixed(2)} pirate credits.`
} else {
return "Failed treasure hunt."
}
}