Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function hero(input){
- let obj={};
- for(let line of input){
- let [command,name,spell]=line.split(" ");
- if(command==="End"){
- break;
- }else if(command==="Enroll"){
- if(!obj.hasOwnProperty(name)){
- obj[name]=[];
- }else {
- console.log(`${name} is already enrolled.`)
- }
- }else if(command==="Learn"){
- if(!obj.hasOwnProperty(name)){
- console.log(`${name} doesn't exist.`);
- }else if(!obj[name].includes(spell)){
- obj[name].push(spell);
- }else if(obj[name].includes(spell)){
- console.log(`${name} has already learnt ${spell}.`);
- }
- }else if(command==="Unlearn"){
- if(!obj.hasOwnProperty(name)){
- console.log(`${name} doesn't exist.`);
- }else if(obj[name]===undefined || !obj[name].includes(spell) ){
- console.log(`${name} doesn't know ${spell}.`);
- }else if(obj[name].includes(spell)){
- obj[name].splice(obj[name].indexOf(spell),1);
- }
- }
- }
- let sorted=Object.entries(obj).sort((a,b)=>b[1].length-a[1].length || a[0].localeCompare(b[0]));
- console.log(`Heroes:`);
- for(let kvp of sorted){
- console.log(`== ${kvp[0]}: ${kvp[1]}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement