Advertisement
Kamend1

01.SpaceCrewManagement

Apr 9th, 2025
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve (commandArr) {
  2.     class Astronaut {
  3.         constructor(name, location) {
  4.             this.name = name;
  5.             this.location = location;
  6.             this.skills = [];
  7.         }
  8.    
  9.         toString() {
  10.             return `Astronaut: ${this.name}, Section: ${this.location}, Skills: ${this.skills.sort().join(', ')}`;
  11.         }
  12.     }
  13.    
  14.  
  15.     let astronautArr = [];
  16.     let numAstronats = Number(commandArr[0]);
  17.  
  18.     for (let idx = 1; idx <= numAstronats; idx++) {
  19.         let astroStr = commandArr[idx];
  20.         let [name, location, skills] = astroStr.split(' ');
  21.         let astronaut = new Astronaut(name, location);
  22.         let skillArr = skills.split(',');
  23.  
  24.         for (let skill of skillArr) {
  25.             astronaut.skills.push(skill);
  26.         }
  27.         astronautArr.push(astronaut);
  28.     }
  29.  
  30.     for (let idx = numAstronats + 1; idx < commandArr.length; idx++) {
  31.         currentCommandTokens = commandArr[idx].split(' / ');
  32.         if (currentCommandTokens[0] === "End") {
  33.             break;
  34.         } else if (currentCommandTokens[0] === "Perform") {
  35.             let [_, name, location, task] = currentCommandTokens;
  36.             let currentAstronaut = astronautArr.find(p => p.name === name);
  37.  
  38.             if (!currentAstronaut) {
  39.                 continue;
  40.             }
  41.  
  42.             if (currentAstronaut.location !== location || !currentAstronaut.skills.includes(task)) {
  43.                 console.log(`${currentAstronaut.name} cannot perform the skill: ${task}.`);
  44.                 continue;
  45.             }
  46.  
  47.             console.log(`${currentAstronaut.name} has successfully performed the skill: ${task}!`)
  48.  
  49.         } else if (currentCommandTokens[0] === "Transfer") {
  50.             let [_, name, location] = currentCommandTokens;
  51.             let currentAstronaut = astronautArr.find(p => p.name === name);
  52.  
  53.             if (!currentAstronaut) {
  54.                 continue;
  55.             }
  56.            
  57.             currentAstronaut.location = location;
  58.             console.log(`${currentAstronaut.name} has been transferred to: ${location}`);
  59.            
  60.  
  61.         } else if (currentCommandTokens[0] === "Learn Skill") {
  62.             let [_, name, task] = currentCommandTokens;
  63.             let currentAstronaut = astronautArr.find(p => p.name === name);
  64.  
  65.             if (!currentAstronaut) {
  66.                 continue;
  67.             }
  68.  
  69.             if (currentAstronaut.skills.includes(task)) {
  70.                 console.log(`${currentAstronaut.name} already knows the skill: ${task}.`);
  71.             } else {
  72.                 currentAstronaut.skills.push(task);
  73.                 console.log(`${currentAstronaut.name} has learned a new skill: ${task}.`);
  74.             }
  75.         }
  76.     }
  77.  
  78.     console.log(astronautArr.join('\n'));
  79.  
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement