Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(args) {
- let commandLine = args.shift();
- const heroes = {};
- while (commandLine !== 'End') {
- const [commmand, heroName, spellName] = commandLine.split(' ');
- switch (commmand) {
- case 'Enroll':
- if (Object.prototype.hasOwnProperty.call(heroes, heroName)) {
- console.log(`${heroName} is already enrolled.`);
- } else {
- heroes[heroName] = heroName;
- heroes[heroName] = [];
- }
- break;
- case 'Learn':
- if (Object.prototype.hasOwnProperty.call(heroes, heroName)) {
- const spellIsFind = heroes[heroName].some((spell) => spell === spellName);
- if (spellIsFind) {
- console.log(`${heroName} has already learnt ${spellName}.`);
- } else {
- heroes[heroName].push(spellName);
- }
- } else {
- console.log(`${heroName} doesn't exist.`);
- }
- break;
- case 'Unlearn':
- if (Object.prototype.hasOwnProperty.call(heroes, heroName)) {
- const spellIsFind = heroes[heroName].some((spell) => spell === spellName);
- if (spellIsFind) {
- const takeIndex = heroes[heroName].indexOf(spellName);
- heroes[heroName] = heroes[heroName].splice(takeIndex, 0);
- } else {
- console.log(`${heroName} doesn't know ${spellName}.`);
- }
- } else {
- console.log(`${heroName} doesn't exist.`);
- }
- break;
- default:
- break;
- }
- commandLine = args.shift();
- }
- console.log('Heroes:');
- const sortedHeroes = Object.entries(heroes).sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]));
- sortedHeroes.forEach((el) => {
- const [heroName, spellbook] = el;
- if (heroes[heroName]) {
- const spells = spellbook.join(', ');
- console.log(`== ${heroName}: ${spells}`);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement