From 4531c2334d39eaaa6343b964bb7eb1a68941bd3d Mon Sep 17 00:00:00 2001 From: Sajad Jalilian Date: Thu, 4 Jan 2024 21:33:26 +0330 Subject: [PATCH] move js functions to another file --- functions.js | 31 +++++++++++++++++++++++++++++++ index.html | 42 +++--------------------------------------- 2 files changed, 34 insertions(+), 39 deletions(-) create mode 100644 functions.js diff --git a/functions.js b/functions.js new file mode 100644 index 0000000..2bccbc8 --- /dev/null +++ b/functions.js @@ -0,0 +1,31 @@ +function fetchData() { + return fetch('data/jalali_imp.min.json') + .then(response => response.json()) + .then(data => { + this.data = data; + }) + .catch(error => { + console.error('Error fetching data:', error); + }); +}; + +function calculateSum(value, date) { + const rawData = this.data; + usdOnDate = rawData[date]["usd"]["sell"]; + + const keys = Object.keys(rawData); + const lastKey = keys[keys.length - 1]; + const lastValue = rawData[lastKey]; + + latestUsdValue = lastValue["usd"]["sell"]; + + usdAtDate = value / usdOnDate; + rialAtLastDate = usdAtDate * latestUsdValue; + + const rounded = Math.round(rialAtLastDate * 100) / 100; + return { value: rounded, lastUsdValue: latestUsdValue, usdValueOnDate: usdOnDate } +}; + +function showData() { + return this.data; +} \ No newline at end of file diff --git a/index.html b/index.html index 82e0cd7..b588e9c 100644 --- a/index.html +++ b/index.html @@ -12,50 +12,14 @@ gtag('config', 'G-VL6DSZLQG3'); + + - - Real Money Calculator + محاسبه‌گر ارزش واقعی پول امروز - - - -