vladovip

Dungeonest Dark _JS Fund_ Arrays

Feb 18th, 2022
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     let health = 100;
  4.     let coins = 0;
  5.     let rooms = input[0].split('|')
  6.  
  7.     for(let i = 0; i < rooms.length; i++){
  8.         let item = rooms[i].split(' ');
  9.  
  10.         if(item[0] === 'chest'){
  11.             coins += Number(item[1]);
  12.             console.log(`You found ${item[1]} coins.`);
  13.         } else if (item[0] === 'potion'){
  14.             health += Number(item[1]);
  15.             if(health > 100){
  16.                 health2 = health - item[1];
  17.                 item[1] = Math.abs(100 - health2);
  18.                 health = 100;
  19.             }
  20.             console.log(`You healed for ${item[1]} hp.`);
  21.             console.log(`Current health: ${health} hp.`);
  22.         } else {
  23.             health -= Number(item[1]);
  24.  
  25.             if(health <= 0){
  26.                 console.log(`You died! Killed by ${item[0]}.`);
  27.                 console.log(`Best room: ${i+1}`);
  28.                 return;
  29.             } else {
  30.                 console.log(`You slayed ${item[0]}.`);
  31.             }
  32.         }
  33.     }
  34.     console.log(`You've made it!`);
  35.    console.log(`Coins: ${coins}`);
  36.    console.log(`Health: ${health}`);
  37. }
  38.  
  39. solve(['rat 10|bat 20|potion 10|rat 10|chest 100|boss 70|chest 1000']);
Advertisement
Add Comment
Please, Sign In to add comment