Advertisement
kstoyanov

02. MuOnline Programming Fundamentals Mid Exam - 29 February

Jul 4th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function exam(args) {
  2.   let initialHealth = 100;
  3.   let initialBitcoins = 0;
  4.    
  5.   const dungeonRooms = args.split('|');
  6.  
  7.   for (let i = 0; i < dungeonRooms.length; i++) {
  8.     const room = dungeonRooms[i];
  9.     const [command, num] = room.split(' ');
  10.  
  11.     if (command === 'potion') {
  12.       let healedWith = Number(num);
  13.  
  14.       if (initialHealth + Number(num) > 100) {
  15.         healedWith = 100 - initialHealth;
  16.         initialHealth = 100;
  17.       } else {
  18.         initialHealth += healedWith;
  19.       }
  20.  
  21.       console.log(`You healed for ${healedWith} hp.`);
  22.       console.log(`Current health: ${initialHealth} hp.`);
  23.     } else if (command === 'chest') {
  24.       initialBitcoins += Number(num);
  25.       console.log(`You found ${num} bitcoins.`);
  26.     } else {
  27.       initialHealth -= Number(num);
  28.       if (initialHealth <= 0) {
  29.         console.log(`You died! Killed by ${command}.`);
  30.         console.log(`Best room: ${i + 1}`);
  31.         break;
  32.       } else {
  33.         console.log(`You slayed ${command}.`);
  34.       }
  35.     }
  36.   }
  37.  
  38.   if (initialHealth > 0) {
  39.     console.log("You've made it!");
  40.     console.log(`Bitcoins: ${initialBitcoins}`);
  41.     console.log(`Health: ${initialHealth}`);
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement