//Reusable Fields const getSellFields = function () { let fields = [] for (var i = 2; i < 14; i++) { fields.push($("#sell_" + i)[0]) } return fields } const sell_inputs = getSellFields() const buy_input = $("#buy") //Functions const fillFields = function (prices) { buy_input.val(prices[0] || '') const sell_prices = prices.slice(2) sell_prices.forEach((price, index) => { if (!price) { return } else { $("#sell_" + (index + 2)).val(price) } }) } const initialize = function () { try { const prices = getPrices() console.log("Prices from Storage", prices) if (prices === null) { return } else { fillFields(prices) } $(document).trigger("input"); } catch (e) { console.error(e); } $("#reset").on("click", function () { $("input").val(null).trigger("input"); }) } const updateLocalStorage = function (data) { try { if (data.length !== 14) throw "The data array needs exactly 14 elements to be valid" localStorage.setItem("sell_prices", JSON.stringify(data)) } catch (e) { console.error(e) } } const isEmpty = function (arr) { const filtered = arr.filter(value => value !== null && value !== '') return filtered.length == 0 } const getPrices = function () { let prices = JSON.parse(localStorage.getItem("sell_prices")) if (!prices || isEmpty(prices) || prices.length !== 14) { return null } else { return prices } } const getSellPrices = function () { //Checks all sell inputs and returns an array with their values return res = sell_inputs.map(function (input) { return input.value || '' }) } const calculateOutput = function (data) { if (isEmpty(data.slice(2))) { $("#output").html(""); return; } let output_possibilities = ""; for (let poss of analyze_possibilities(data)) { var out_line = "" + poss.pattern_description + "" for (let day of poss.prices.slice(1)) { if (day.min !== day.max) { out_line += `${day.min}..${day.max}`; } else { out_line += `${day.min}`; } } out_line += `${poss.weekMin}${poss.weekMax}`; output_possibilities += out_line } $("#output").html(output_possibilities) } const update = function () { const sell_prices = getSellPrices() const buy_price = buy_input.val() const data = [buy_price, buy_price, ...sell_prices] updateLocalStorage(data) calculateOutput(data) } $(document).ready(initialize); $(document).on("input", update);