Liliana797979

mu online - mid exam - fundamentals

May 23rd, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function muOnline(inputArr) {
  3.     let copyArr = inputArr.slice().split('|');
  4.  
  5.  
  6.     let maxHealth = 100;
  7.     let currentHealth = 100;
  8.     let bitcoins = 0;
  9.     let isDead = false;
  10.     let roomCount = 1;
  11.  
  12.     for (let i = 0; i < copyArr.length; i++) {
  13.         let currentRoom = copyArr[i].slice().split(' ');
  14.         let action = currentRoom[0];
  15.         let num = Number(currentRoom[1]);
  16.  
  17.         if (isDead) {
  18.             break;
  19.         }
  20.  
  21.         switch (action) {
  22.             case 'potion':
  23.                 potion(num)
  24.                 break;
  25.             case 'chest':
  26.                 chest(num)
  27.                 break;
  28.             default:
  29.                 figth(action, num)
  30.                 break;
  31.         }
  32.         roomCount++
  33.     }
  34.  
  35.     if (!isDead) {
  36.         console.log(`You've made it!\nBitcoins: ${bitcoins}\nHealth: ${currentHealth}`);
  37.    }
  38.  
  39.    function potion(health) {
  40.        if (currentHealth + health <= 100) {
  41.            currentHealth += health;
  42.            console.log(`You healed for ${health} hp.`);
  43.        } else {
  44.            console.log(`You healed for ${maxHealth - currentHealth} hp.`);
  45.            currentHealth = maxHealth;
  46.        }
  47.        console.log(`Current health: ${currentHealth} hp.`);
  48.    }
  49.  
  50.    function chest(bitcoin) {
  51.        bitcoins += bitcoin;
  52.        console.log(`You found ${bitcoin} bitcoins.`);
  53.    }
  54.  
  55.    function figth(monster, attack) {
  56.        currentHealth -= attack;
  57.        if (currentHealth <= 0) {
  58.            isDead = true;
  59.            console.log(`You died! Killed by ${monster}.`);
  60.            console.log(`Best room: ${roomCount}`);
  61.        } else {
  62.            console.log(`You slayed ${monster}.`);
  63.        }
  64.    }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment