Liliana797979

viarno reshenie mu online - mid exam - fundamenals

Jul 16th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.      
  3. function muOnline(input) {
  4.     let array = input.split("|");
  5.  
  6.     let initialHealth = 100;
  7.     let totalBitcoins = 0;
  8.     let roomCounter = 0;
  9.     let monster = "";
  10.  
  11.     for (let i = 0; i < array.length; i++) {
  12.         let tokens = array[i].split(" ");
  13.  
  14.         if (tokens[0] == "potion") {
  15.             roomCounter++;
  16.          
  17.             let healingPart = Number.parseInt(tokens[1]);
  18.             let currentHealth = initialHealth + healingPart;
  19.          
  20.             if (currentHealth < 100) {
  21.                 initialHealth += healingPart;
  22.                 console.log(`You healed for ${healingPart} hp.`);
  23.                 console.log(`Current health: ${currentHealth} hp.`);
  24.             } else {
  25.                 let potionOfHealingPart = (100 + healingPart) - currentHealth;
  26.                 initialHealth += potionOfHealingPart;
  27.                 console.log(`You healed for ${potionOfHealingPart} hp.`);
  28.                 console.log(`Current health: ${initialHealth} hp.`);
  29.             }
  30.         } else if (tokens[0] == "chest") {
  31.             roomCounter++;
  32.          
  33.             let numberOfBitcoins = Number.parseInt(tokens[1]);
  34.             console.log(`You found ${numberOfBitcoins} bitcoins.`);
  35.          
  36.             totalBitcoins += numberOfBitcoins;
  37.         } else {
  38.             roomCounter++;
  39.  
  40.             monster = tokens[0];
  41.             let attack = Number.parseInt(tokens[1]);
  42.             initialHealth -= attack;
  43.             if (initialHealth > 0) {
  44.                 console.log(`You slayed ${monster}.`);
  45.             } else {
  46.                 console.log(`You died! Killed by ${monster}.`);
  47.                 console.log(`Best room: ${roomCounter}`);
  48.                 return;
  49.             }
  50.         }
  51.     }
  52.  
  53.     console.log(`You've made it!\nBitcoins: ${totalBitcoins}\nHealth: ${initialHealth}`);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment