Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

e.atanasova avatar e.atanasova 5 Точки

03. Heroes of Code and Logic VII

Здравейте,

На задачата получавам 66/100. Мисля, че проблемът е в сортирането, но не съм сигурна как да го поправя.

Това е кодът ми: https://pastebin.com/fxFDHpHJ 

Условие на задачата: https://softuni.bg/trainings/resources/officedocument/64338/dokument-uslovie-programming-fundamentals-with-javascript-september-2021/3449 

Благодаря предварително.

0
JavaScript Fundamentals 12/08/2022 11:46:36
Axiomatik avatar Axiomatik 2422 Точки
Best Answer

2 Demo codes for comparison =>

;-)

Code #1:

function solve(input) {
    let num = +input.shift();
    let index = 0;
    let arrObjects = [];
    let current = input.shift();

    while (index < num) {
        let obj = {};
        let [hero, hitPoints, manaPoints] = current.split(' ');
        hitPoints = +hitPoints;
        manaPoints = +manaPoints;

        obj = {
            hero,
            hitPoints,
            manaPoints
        };

        arrObjects.push(obj)
        current = input.shift();
        index++;
    }

    while (current !== 'End') {
        if (current.includes('CastSpell')) {
            let [commandName, heroName, manaPoinstNeeded, spellName] = current.split(' - ');
            arrObjects.filter(el => {
                if (el.hero == heroName) {
                    let result = el.manaPoints - manaPoinstNeeded;

                    // if (el.manaPoints >= manaPoinstNeeded && el.manaPoints <= 200) {
                    if (el.manaPoints >= manaPoinstNeeded) {
                        el.manaPoints -= manaPoinstNeeded;
                        console.log(`${el.hero} has successfully cast ${spellName} and now has ${result} MP!`)
                    } else {
                        console.log(`${el.hero} does not have enough MP to cast ${spellName}!`)
                    }
                }
            });
        } else if (current.includes('TakeDamage')) {
            let [commandName, heroName, damage, attacker] = current.split(' - ');
            arrObjects.filter(el => {
                if (el.hero == heroName) {
                    damage = +damage;

                    if (el.hitPoints > damage) {
                        el.hitPoints -= damage;
                        console.log(`${el.hero} was hit for ${damage} HP by ${attacker} and now has ${el.hitPoints} HP left!`)
                    } else {
                        console.log(`${el.hero} has been killed by ${attacker}!`)
                        let index = arrObjects.indexOf(el);
                        arrObjects.splice(index, 1);
                    }
                }
            });
        } else if (current.includes('Heal')) {
            let [commandName, heroName, amount] = current.split(' - ');
            amount = +amount;
            arrObjects.filter(el => {
                if (el.hero == heroName) {
                    let result = el.hitPoints + amount;
                    if (result <= 100) {
                        el.hitPoints += amount;
                        console.log(`${el.hero} healed for ${amount} HP!`)
                    } else if (result > 100) {
                        // let residue = result - 100;
                        // amount = Math.abs(amount - residue);
                        let residue = 100 - el.hitPoints;
                        el.hitPoints = 100;
                        console.log(`${el.hero} healed for ${residue} HP!`)
                        // console.log(`${el.hero} healed for ${amount} HP!`)
                    }
                }
            })
        } else if (current.includes('Recharge')) {
            let [commandName, heroName, amount] = current.split(' - ');
            amount = +amount;
            arrObjects.filter(el => {
                if (el.hero == heroName) {
                    let result = el.manaPoints + amount;

                    if (result > 200) {
                        // let residue = result - amount;
                        // amount = Math.abs(amount - residue);
                        let residue = 200 - el.manaPoints;
                        el.manaPoints = 200;
                        console.log(`${el.hero} recharged for ${residue} MP!`)
                    } else {
                        el.manaPoints += amount;
                        console.log(`${el.hero} recharged for ${amount} MP!`)
                    }
                }
            })
        }

        current = input.shift();
    }

    for (let line of arrObjects) {
        console.log(`${line.hero}
  HP: ${line.hitPoints}
  MP: ${line.manaPoints}`)
    }
    //console.log(arrObjects)

}

 

Code #2:

function solve(input) {
    // 'Solmyr 85 120',
    // 'Kyrre 99 50',

    // 'Heal - Solmyr - 10',
    // 'Recharge - Solmyr - 50',
    // 'TakeDamage - Kyrre - 66 - Orc',
    // 'CastSpell - Kyrre - 15 - ViewEarth',

    // 'End'

    // 4
    // Adela 90 150
    // SirMullich 70 40
    // Ivor 1 111
    // Tyris 94 61

    // Heal - SirMullich - 50
    // Recharge - Adela - 100
    // CastSpell - Tyris - 1000 - Fireball
    // TakeDamage - Tyris - 99 - Fireball
    // TakeDamage - Ivor - 3 - Mosquito
    // End

    let actions = {
        'CastSpell': castSpell,
        'TakeDamage': takeDamage,
        'Recharge': recharge,
        'Heal': heal,
    }

    let numberOfHeroes = Number(input.shift());
    let heroes = {};

    for (let i = 0; i < numberOfHeroes; i++) {
        // Solmyr 85 120',
        let [heroName, hitPoints, manaPoints] = input.shift().split(' ');
        hitPoints = Number(hitPoints);
        manaPoints = Number(manaPoints);

        heroes[heroName] = {
            hitPoints,
            manaPoints
        }
    }

    while (input[0] !== 'End') {

        let tokens = input.shift().split(' - ');
        let command = tokens[0];
        let action = actions[command];
        action(tokens[1], tokens[2], tokens[3]);
    }

    function heal(heroName, amount) {
        // Heal - Solmyr - 10',

        // Heal – {hero name} – {amount}
        // •	The hero increases his HP. If a command is given that would bring the HP of the hero above the maximum value (100), HP is increased to 100 (the HP can’t go over the maximum value).
        // •	 Print the following message:
        // o	"{hero name} healed for {amount recovered} HP!"
        amount = Number(amount);
        let hero = heroes[heroName]; // we have 70. they heal us with 50. max heal is 100. so we need to heal with (100 - 70). we have 20 on top - we need to remove 20 
        let oldValue = hero.hitPoints;
        hero.hitPoints = Math.min(100, hero.hitPoints + amount);
        console.log(`${heroName} healed for ${hero.hitPoints - oldValue} HP!`);
    }

    function castSpell(heroName, mpNeeded, spellName) {
        mpNeeded = Number(mpNeeded);
        //         CastSpell – {hero name} – {MP needed} – {spell name} 
        // •	If the hero has the required MP, he casts the spell, thus reducing his MP. Print this message: 
        // o	"{hero name} has successfully cast {spell name} and now has {mana points left} MP!"
        // •	If the hero is unable to cast the spell print:
        // o	"{hero name} does not have enough MP to cast {spell name}!"
        // 'CastSpell - Kyrre - 15 - ViewEarth',
        let hero = heroes[heroName];

        if (hero.manaPoints >= mpNeeded) {
            hero.manaPoints -= mpNeeded;

            console.log(`${heroName} has successfully cast ${spellName} and now has ${hero.manaPoints} MP!`);
            // Kyrre has successfully cast ViewEarth and now has 35 MP!
        } else {
            console.log(`${heroName} does not have enough MP to cast ${spellName}!`)
        }
    }

    function takeDamage(heroName, damage, attacker) {
        //      TakeDamage – {hero name} – {damage} – {attacker}
        // •	Reduce the hero HP by the given damage amount. If the hero is still alive (his HP is greater than 0) print:
        // o	"{hero name} was hit for {damage} HP by {attacker} and now has {current HP} HP left!"
        // •	If the hero has died, remove him from your party and print:
        // o	"{hero name} has been killed by {attacker}!"
        // TakeDamage - Kyrre - 66 - Orc',
        damage = Number(damage);
        let hero = heroes[heroName];

        hero.hitPoints -= damage;

        if (hero.hitPoints > 0) {
            console.log(`${heroName} was hit for ${damage} HP by ${attacker} and now has ${hero.hitPoints} HP left!`);

        } else {
            delete heroes[heroName];
            console.log(`${heroName} has been killed by ${attacker}!`);
        }
    }

    function recharge(heroName, amount) {
        amount = Number(amount);
        // Recharge - Solmyr - 50'
        // Recharge – {hero name} – {amount}
        // •	The hero increases his MP. If a command is given that would bring the MP of the hero above the maximum value (200), MP is increased to 200. (the MP can’t go over the maximum value).
        // •	 Print the following message:
        // o	"{hero name} recharged for {amount recovered} MP!"
        let hero = heroes[heroName];

        let oldValue = hero.manaPoints;
        hero.manaPoints = Math.min(200, hero.manaPoints + amount);

        console.log(`${heroName} recharged for ${hero.manaPoints - oldValue} MP!`);
    }

    // Solmyr healed for 10 HP!
    // Solmyr recharged for 50 MP!
    // Kyrre was hit for 66 HP by Orc and now has 33 HP left!
    // Kyrre has successfully cast ViewEarth and now has 35 MP!
    // Solmyr
    //   HP: 95   / descending order 
    //   MP: 170
    // Kyrre
    //   HP: 33
    //   MP: 35

    // SirMullich healed for 30 HP!
    // Adela recharged for 50 MP!
    // Tyris does not have enough MP to cast Fireball!
    // Tyris has been killed by Fireball!
    // Ivor has been killed by Mosquito!
    // SirMullich
    //   HP: 100
    //   MP: 40
    // Adela
    //   HP: 90
    //   MP: 200

    //     Print all members of your party who are still alive, sorted by their HP in descending order (b – a) , then by their name in ascending order (a – b) , in the following format (their HP/MP need to be indented 2 spaces):
    // "{hero name}
    //      HP: {current HP}
    //      MP: {current MP}

    // [
    //     [ 'Adela', { hitPoints: 90, manaPoints: 200 } ],
    //     [ 'SirMullich', { hitPoints: 100, manaPoints: 40 } ]
    //   ]

    let sortedHeroes = Object.entries(heroes).
        sort(sortingHeroes);

    function sortingHeroes(a, b) {

        let [aName, aInfo] = a;
        let [bName, bInfo] = b;

        let byHealthDescending = bInfo.hitPoints - aInfo.hitPoints;

        if (byHealthDescending === 0) {
            return aName.localeCompare(bName);
        }

        return byHealthDescending;
    }

    // for (let [heroName, hero] of sortedHeroes) {
    for (let [heroName, hero] of Object.entries(heroes)) {

        console.log(`${heroName}`);
        console.log(`  HP: ${hero.hitPoints}`);
        console.log(`  MP: ${hero.manaPoints}`);
    }
}

 

0
e.atanasova avatar e.atanasova 5 Точки

Thank you so much!

1
12/08/2022 13:42:11
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.