JSFiddle - React, Tailwind, and code Playground

by Helmut Granda

JavaScript

var Print = function () {
  var fullString = "";
 
  function clear() {
    fullString = "";
  }
 
  function print(value) {
    fullString += value.toString();
  }
 
  function printLine(value) {
    if (value)
      console.log(value);
    console.log("\n");
  }
 
  function display() {
    Abort(fullString + "\n");
  }
 
  return {
    clear: clear,
    print: print,
    printLine: printLine,
    display: display
  };
}();



function HealingItem(name, stat, amount) {
  return function () {
    var oldStat = hero[stat];
    hero[stat] += amount;
    if (hero[stat] > hero[stat + "Max"]) hero[stat] = hero[stat + "Max"];
 
    var heal = hero[stat] - oldStat;
    if (heal > 0)
      Print.printLine("Drank " + name + "! Recovered " + heal + " " + stat.toUpperCase() + ".");
    else
      Print.printLine("Drank " + name + "! Nothing happened.");
  };
}
 
function Potion() {
  return HealingItem("Potion", "hp", 10);
}
 
function SuperPotion() {
  return HealingItem("Super Potion", "hp", 20);
}
 
function MegaPotion() {
  return HealingItem("Mega Potion", "hp", 50);
}

function ArchPotion() {
  return HealingItem("Arch Potion", "hp", 100);
}


var hero = {hp: 10, hpMax: 150};

var inventory = [Potion(), SuperPotion(), MegaPotion(), ArchPotion()];
inventory[0];
inventory[1]();
inventory[2]();
inventory[3]();
inventory[0]();

Print.printLine(hero.hp);