Advertisement
divanov94

Untitled

Aug 5th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function hero(input){
  2.  
  3.     let obj={};
  4.  
  5.  
  6.     for(let line of input){
  7.         let [command,name,spell]=line.split(" ");
  8.         if(command==="End"){
  9.             break;
  10.         }else if(command==="Enroll"){
  11.             if(!obj.hasOwnProperty(name)){
  12.                 obj[name]=[];
  13.             }else {
  14.                 console.log(`${name} is already enrolled.`)
  15.             }
  16.  
  17.         }else if(command==="Learn"){
  18.             if(!obj.hasOwnProperty(name)){
  19.                 console.log(`${name} doesn't exist.`);
  20.            }else if(!obj[name].includes(spell)){
  21.                obj[name].push(spell);
  22.            }else if(obj[name].includes(spell)){
  23.                console.log(`${name} has already learnt ${spell}.`);
  24.            }
  25.  
  26.        }else if(command==="Unlearn"){
  27.            if(!obj.hasOwnProperty(name)){
  28.                console.log(`${name} doesn't exist.`);
  29.             }else if(obj[name]===undefined || !obj[name].includes(spell) ){
  30.                 console.log(`${name} doesn't know ${spell}.`);
  31.            }else if(obj[name].includes(spell)){
  32.                obj[name].splice(obj[name].indexOf(spell),1);
  33.            }
  34.  
  35.        }
  36.    }
  37.    let sorted=Object.entries(obj).sort((a,b)=>b[1].length-a[1].length || a[0].localeCompare(b[0]));
  38.    console.log(`Heroes:`);
  39.    for(let kvp of sorted){
  40.        console.log(`== ${kvp[0]}: ${kvp[1]}`);
  41.    }
  42.  
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement