Advertisement
mahlichavpm

Untitled

Mar 13th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dungeon (inputArr) {
  2.  
  3.     let newArr = inputArr[0].split('|');
  4.     let roomCount = 1;
  5.     let health = 100;
  6.     let coins = 0;
  7.     let flag = false;
  8.  
  9.     let command = [];
  10.    
  11.    
  12.     for(let i = 0; i < newArr.length; i++) {
  13.         command = newArr[i].split(' ');
  14.  
  15.         if (command[0] == 'potion'){
  16.            
  17.             health += Number(command[1]);
  18.             if (health > 100) {
  19.                 let excess = health - 100;
  20.                  command[1] = Number(command[1]) - excess;
  21.                 health = 100;
  22.             }
  23.             console.log(`You healed for ${command[1]} hp.`);
  24.             console.log(`Current health: ${health} hp.`);
  25.             roomCount ++;
  26.  
  27.         } else if (command[0] == 'chest') {
  28.  
  29.             coins += Number(command[1]);
  30.             console.log(`You found ${command[1]} coins.`);
  31.             roomCount++;
  32.  
  33.         } else {
  34.             health -= Number(command[1]);
  35.             if (health <= 0) {
  36.                 console.log(`You died! Killed by ${command[0]}.`);
  37.                 console.log(`Best room: ${roomCount}`);
  38.                 flag = true;
  39.                 break;
  40.             } else {
  41.                 console.log(`You slayed ${command[0]}.`);
  42.                 roomCount++;
  43.             }
  44.         }
  45.     }
  46.  
  47.     if (!flag) {
  48.         console.log('You\'ve made it!');
  49.         console.log(`Coins: ${coins}`);
  50.         console.log(`Health: ${health}`);
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement