Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(str) {
  2.     let rooms = str[0].split('|');
  3.     let health = 100;
  4.     let coins = 0;
  5.     let dead = false;
  6.  
  7.     for (let i = 0; i < rooms.length; i++) {
  8.         let [item, num] = rooms[i].split(' ');
  9.  
  10.         if (item === 'potion') {
  11.  
  12.             if (health + Number(num) > 100) {
  13.                 console.log(`You healed for ${100 - health} hp.`);
  14.                 health = 100;
  15.             } else {
  16.                 health += Number(num);
  17.                 console.log(`You healed for ${num} hp.`);
  18.             }
  19.  
  20.             console.log(`Current health: ${health} hp.`);
  21.            
  22.         } else if (item === 'chest') {
  23.             coins += Number(num);
  24.             console.log(`You found ${num} coins.`);
  25.         } else {
  26.             health -= Number(num);
  27.  
  28.             if (health > 0) {
  29.                 console.log(`You slayed ${item}.`);
  30.             } else {
  31.                 console.log(`You died! Killed by ${item}.`);
  32.                 console.log(`Best room: ${i + 1}`);
  33.                 dead = true;
  34.                 break;
  35.             }
  36.         }
  37.     }
  38.  
  39.     if (!dead) {
  40.         console.log("You've made it!");
  41.         console.log(`Coins: ${coins}`);
  42.         console.log(`Health: ${health}`);
  43.     }
  44. }
  45.  
  46. solve(['cat 10|potion 30|orc 10|chest 10|snake 25|chest 110']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement