diff --git a/index.html b/index.html index f7295a5..b9dd190 100644 --- a/index.html +++ b/index.html @@ -4,11 +4,52 @@ - + + D&D Currency Converter

D&D Currency Converter

+
+ + ---------> + +
+ +
+
+
+
+ +
+
+

+
+
+



+
+ Arrow icon made by + Lyolya + from flaticon + is licensed by + + CC 3.0 BY +
+
+ Source is auditable here. +
\ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..4823010 --- /dev/null +++ b/script.js @@ -0,0 +1,65 @@ +const value_dict = { + 0: "Bronze", + 1: "Silver", + 2: "Gold", + 3: "Platinum" +} + + + +const calculate_remainder = (amount, delta) => { + if (delta < 0) { + return amount % 10 ** Math.abs(delta) + } else { + return 0 + } +}; + + +const convert_to = (amount, delta) => { + return Math.floor(amount * 10 ** delta) +}; + + + +const convert = () => { + let from_currency = parseInt(document.getElementById("from-in").value); + let to_currency = parseInt(document.getElementById("to-in").value); + let value = parseInt(document.getElementById("amount").value) + let response_area = document.getElementById("response-area"); + + let delta = from_currency - to_currency + + + // Ensure that they have selected coins so we can convert + if (isNaN(from_currency) || isNaN(to_currency)) { + response_area.innerText = "ERROR: Both sides of the conversion must have a selected coin type." + return + } + + + // Ensure we aren't converting to the same type of coin + if (delta === 0) { + response_area.innerText = "ERROR: Cannot convert from one coin to the same coin" + return + } + + + const remainder = calculate_remainder(value, delta); + const amount = convert_to(value, delta); + + + let response = ""; + + + response += `${value_dict[to_currency]}: ${amount}` + + + // Check if we can't go into a whole number of the new coins + if (remainder !== 0) { + response += `
${value_dict[from_currency]}: ${remainder}` + } + + + response_area.innerHTML = response +}; \ No newline at end of file