Advertisement
Silviya7

AstroAdventure

May 15th, 2024
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3. let n= input.shift();//Number Astrounafts
  4.  
  5.  
  6. let AllAstrounafts={};
  7. for (let i = 0; i <n; i++) {
  8.    
  9.     const[astoname, oxygen1, energy1]=(input.shift()).split(' ');
  10.    let oxygen= Number(oxygen1);
  11.    let energy= Number(energy1);
  12.     AllAstrounafts[astoname]={oxygen,energy};
  13.    
  14. }
  15.  
  16.  
  17. let LineCmd= input.shift();
  18.  
  19. while(LineCmd !='End'){
  20.  
  21.     const All=LineCmd.split(' - ');
  22.     const Cmd= All[0];
  23.     let astroname=All[1];
  24.  
  25.     switch(Cmd)
  26.     {
  27.  
  28.     case 'Explore':
  29.        
  30.         let energyneeded=Number(All[2]);
  31.         if( (AllAstrounafts[astroname].energy) >= energyneeded){
  32.             AllAstrounafts[astroname].energy -=energyneeded;
  33.  
  34.             console.log(`${astroname} has successfully explored a new area and now has ${AllAstrounafts[astroname].energy} energy!`)
  35.         }
  36.         else{
  37.  
  38.             console.log(`${astroname} does not have enough energy to explore!`)
  39.  
  40.         }
  41.     break;
  42.  
  43.     case 'Refuel':
  44.         let neededenergy=200- AllAstrounafts[astroname].energy;
  45.         let amount1=Number(All[2]);
  46.  
  47.         if((AllAstrounafts[astroname].energy + amount1) >200){
  48.             AllAstrounafts[astroname].energy =200;
  49.  
  50.             console.log(`${astroname} refueled their energy by ${neededenergy}!`);
  51.         }
  52.         else{
  53.             AllAstrounafts[astroname].energy +=amount1;
  54.             console.log(`${astroname} refueled their energy by ${amount1}!`);
  55.         }
  56.  
  57.        
  58.  
  59.     break;
  60.  
  61.     case 'Breathe':
  62.         let neededoxygen=100- AllAstrounafts[astroname].oxygen;
  63.         let amount2=Number(All[2]);
  64.  
  65.         if((AllAstrounafts[astroname].oxygen + amount2)> 100){
  66.             AllAstrounafts[astroname].oxygen=100;
  67.  
  68.             console.log(`${astroname} took a breath and recovered ${neededoxygen} oxygen!`)
  69.         }
  70.         else{
  71.  
  72.             AllAstrounafts[astroname].oxygen +=amount2;
  73.  
  74.             console.log(`${astroname} took a breath and recovered ${amount2} oxygen!`)
  75.         }
  76.  
  77.      
  78.     break;
  79.     }
  80.  
  81.     LineCmd= input.shift();
  82. }
  83.  
  84.  
  85. letsortedAstronafts= Object.entries(AllAstrounafts);
  86.  
  87.  
  88. for (const astronaft of letsortedAstronafts) {
  89.    
  90.     console.log(`Astronaut: ${astronaft[0]}, Oxygen: ${astronaft[1].oxygen}, Energy: ${astronaft[1].energy}`);
  91. }
  92.  
  93. }
  94. solve([  '3',
  95. 'John 50 120',
  96. 'Kate 80 180',
  97. 'Rob 70 150',
  98.  
  99. 'Explore - John - 50',
  100. 'Refuel - Kate - 30',
  101. 'Breathe - Rob - 20',
  102. 'End'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement