nikolayneykov

Untitled

Mar 2nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let splited = input[0].split("|");
  4.     let initial = [100, 100];
  5.  
  6.     for (const element of splited) {
  7.         let splitedElement = element.split("-");
  8.  
  9.         if (splitedElement[0] === "rest") {
  10.             let currentHealth = initial[0];
  11.             initial[0] += Number(splitedElement[1]);
  12.  
  13.             if (initial[0] > 100) {
  14.                 console.log(`You gained ${100 - currentHealth} energy.`);
  15.                 console.log(`Current energy: ${100}.`);
  16.                 initial[0] = 100;
  17.             } else {
  18.                 console.log(`You gained ${Number(splitedElement[1])} energy.`);
  19.                 console.log(`Current energy: ${initial[0]}.`);
  20.             }
  21.         } else if (splitedElement[0] === "order") {
  22.             initial[1] += Number(splitedElement[1]);
  23.             initial[0] -= 30;
  24.             if (initial[0] >= 0) {
  25.                 console.log(`You earned ${splitedElement[1]} coins.`);
  26.             } else if (initial[0] <= 0) {
  27.                 initial[1] -= Number(splitedElement[1]);
  28.                 initial[0] += 80;
  29.                 console.log(`You had to rest!`);
  30.             }
  31.  
  32.         } else {
  33.             initial[1] -= Number(splitedElement[1]);
  34.             if (!(initial[1] <= 0)) {
  35.                 console.log(`You bought ${splitedElement[0]}.`);
  36.  
  37.             } else {
  38.                 console.log(`Closed! Cannot afford ${splitedElement[0]}.`);
  39.                 return;
  40.  
  41.             }
  42.  
  43.         }
  44.  
  45.     }
  46.     if (initial[0] > 0) {
  47.         console.log(`Day completed!`);
  48.         console.log(`Coins: ${initial[1]}`);
  49.         console.log(`Energy: ${initial[0]}`);
  50.        
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment