Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr) {
- let rooms = arr[0].split("|");
- let health = 100;
- let coins = 0;
- let isKilled = false;
- for (let i = 0; i < rooms.length; i++) {
- let room = rooms[i].split(" ");
- if (room[0] === "potion") {
- health + Number(room[1]) > 100 ? room[1] = 100 - health : room[1];
- health += Number(room[1]);
- console.log(`You healed for ${room[1]} hp.\nCurrent health: ${health} hp.`);
- } else if (room[0] === "chest") {
- coins += Number(room[1]);
- console.log(`You found ${room[1]} coins.`)
- } else {
- health -= Number(room[1]);
- if (health > 0) {
- console.log(`You slayed ${room[0]}.`)
- } else {
- console.log(`You died! Killed by ${room[0]}.\nBest room: ${i + 1}`);
- isKilled = true;
- break;
- }
- }
- }
- console.log(isKilled ?``: `You've made it!\nCoins: ${coins}\nHealth: ${health}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement