Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(str) { //100т.
- let health = 100;
- let coins = 0;
- let word = '';
- let number = 0;
- let roomCounter = 0;
- let healthDiff = 0;
- let died = false;
- let arr = str[0].split(/[\s|]+/);
- //['rat', '10', 'bat', '20', 'potion', '10', 'rat', '10', 'chest', '100', 'boss', '70', 'chest', '1000'];
- //['cat', '10', 'potion', '30', 'orc', '10', 'chest', '10', 'snake', '25', 'chest', '110'];
- for (let i = 0; i < arr.length - 1; i += 2) {
- word = arr[i];
- number = Number(arr[i + 1]);
- roomCounter++;
- switch (word) {
- case 'chest':
- console.log(`You found ${number} coins.`);
- coins += number;
- break;
- case 'potion':
- if (number >= 100 - health) {
- healthDiff = 100 - health;
- } else {
- healthDiff = number;
- }
- if (health + number > 100) {
- health = 100;
- } else {
- health += number;
- }
- console.log(`You healed for ${healthDiff} hp.`);
- console.log(`Current health: ${health} hp.`);
- break;
- default: //враг
- health -= number; //взима ни живот
- if (health > 0) {
- console.log(`You slayed ${word}.`)
- } else { //влизаме, ако сме умрели
- console.log(`You died! Killed by ${word}.`);
- console.log(`Best room: ${roomCounter}`);
- died = true;
- }
- break;
- }
- if (died) {
- break;
- }
- }
- if (!died) {
- console.log(`You've made it!`);
- console.log(`Coins: ${coins}`);
- console.log(`Health: ${health}`);
- }
- }
- solve(["rat 10|bat 20|potion 10|rat 10|chest 100|boss 70|chest 1000"]);
- console.log();
- solve(["cat 10|potion 30|orc 10|chest 10|snake 25|chest 110"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement