JSFiddle - React, Tailwind, and code Playground
by Darker
HTML
<div id="result"></div>
JavaScript
var gold = 666;
//D'oh an object!
var prices = {
helmet: 150,
armor: 430,
ice_cream: 10,
charm: 5
}
//Define what we want to buy
var wanted = "ice_cream";
//Prevent accessing undefined price
if(prices[wanted]==null)
throw new Error("We don't have this thing in stock yet!");
//Divide the cash by the price to know the amount
var amount = Math.floor(gold/prices[wanted]);
//And calculate spare cash - modulo calculates rest after division
var spare = gold%prices[wanted]
//Finally find out how many charms can be bought with the rest gold
var charms = Math.floor(spare/prices.charm);
//and let the user know
document.getElementById("result").innerHTML = ("With "+gold+" gold, you can buy "+amount+" "+wanted+"(s) and "+charms+" charms.");