Advertisement
Guest User

Untitled

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