Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (input) => { // function [ START ]
- let health = 100, coins = 0;
- let rooms = input.shift().split('|');
- let len = rooms.length;
- for(let i=1; i<=len; i++) {
- let parts = rooms.shift().split(' ');
- let type = parts[0], value = Number(parts[1]);
- if(type === 'potion') {
- let potentialHealth = health + value;
- let newHealth = potentialHealth < 100 ? potentialHealth : 100;
- console.log(`You healed for ${newHealth-health} hp.`);
- health = newHealth;
- console.log(`Current health: ${health} hp.`);
- } else if(type === 'chest') {
- coins += value;
- console.log(`You found ${value} coins.`);
- } else { // facing a monster
- health -= value;
- if(health > 0) {
- console.log(`You slayed ${type}.`);
- } else {
- console.log(`You died! Killed by ${type}.`);
- console.log(`Best room: ${i}`);
- break;
- }
- }
- } // for loop
- if(health > 0) {
- console.log("You've made it!");
- console.log(`Coins: ${coins}`);
- console.log(`Health: ${health}`);
- }
- } // function [ END ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement