Advertisement
knoteva

Untitled

Sep 5th, 2019
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function dungeonDark(arr){
  2.   let dungen = arr[0].split('|');
  3.   let health = 100;
  4.   let coins = 0;
  5.   let counterRooms = 0;
  6.   let allCoins = 0;
  7.  
  8.   for(let i = 0; i < dungen.length;i++){
  9.       let currentRoom = dungen[i].split(' ');
  10.       let command = currentRoom[0];
  11.       let num = Number(currentRoom[1]); // Може още тук да го направим число
  12.       counterRooms++;
  13.  
  14.       if(command === 'potion'){
  15. //Коригирана проверка за здравето
  16.         if (health + num > 100) {
  17.           num = 100 - health;
  18.           health = 100;
  19.         } else
  20.         {
  21.           health += num;
  22.         }
  23.         console.log(`You healed for ${num} hp.`);
  24.         console.log(`Current health: ${health} hp.`);
  25.  
  26.                
  27.       }else if(command === 'chest'){
  28.         coins = Number(num);
  29.         allCoins += Number(num);
  30.         console.log(`You found ${coins} coins.`);
  31.  
  32.       }else{
  33.         health -= Number(num);
  34.         if(health > 0){
  35.           console.log(`You slayed ${command}.`);
  36.         }else{
  37.           console.log(`You died! Killed by ${command}.`);
  38.           console.log(`Best room: ${counterRooms}`);
  39.           //Може направо да прекъснем програмата
  40.           return;
  41.         }
  42.        
  43.       }          
  44.   }
  45.   // Като използваме return проврката се обесмисля
  46.     console.log(`You've made it!`);
  47.    console.log(`Coins: ${allCoins}`);
  48.    console.log(`Health: ${health}`);
  49. }
  50.  
  51. dungeonDark(['rat 10|bat 20|potion 10|rat 10|chest 100|boss 70|chest 1000']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement