add stdev; toggle sources

This commit is contained in:
2023-12-07 05:25:45 +01:00
parent 0d596a9506
commit facb944320

View File

@@ -53,9 +53,28 @@ const get = async (url) => {
return await response.json(); return await response.json();
}; };
const filterPrices = () => {
return prices.value.filter(val => {
return typeof val === 'number';
});
};
const calcAverage = () => { const calcAverage = () => {
const arr = prices.value; const prices = filterPrices();
average.value = arr.reduce((sum, val) => sum + val, 0) / arr.length; average.value = prices.reduce((sum, val) => sum + val, 0) / prices.length;
};
const calcStdev = () => {
const av = average.value;
const prices = filterPrices();
stdev.value = (filterPrices().reduce((sum, price) => {
return sum + (av - price) ** 2;
}, 0) / prices.length) ** 0.5;
};
const recalc = () => {
calcAverage();
calcStdev();
}; };
const format = price => { const format = price => {
@@ -64,6 +83,8 @@ const format = price => {
const average = ref(); const average = ref();
const prices = ref([]); const prices = ref([]);
const stdev = ref();
const showSources = ref(true);
(() => { (() => {
let idx = -1; let idx = -1;
@@ -72,8 +93,9 @@ const prices = ref([]);
idx = (idx + 1) % dataSources.length; idx = (idx + 1) % dataSources.length;
try { try {
const source = dataSources[idx]; const source = dataSources[idx];
prices.value[idx] = null;
prices.value[idx] = source.pick(await get(source.url)); prices.value[idx] = source.pick(await get(source.url));
calcAverage(); recalc();
} catch (e) {}; } catch (e) {};
}; };
loop(); loop();
@@ -88,12 +110,16 @@ const prices = ref([]);
<template> <template>
<p>1 BTC = {{ format(average) }} USD</p> <p>1 BTC = {{ format(average) }} USD</p>
<p>Data Sources:</p> <button @click="showSources = !showSources">Toggle Sources</button>
<ul> <div v-show="showSources">
<li v-for="(val, i) in dataSources"> <p>Data Sources:</p>
{{ val.name }}: {{ format(prices[i]) }} <ul>
</li> <li v-for="(val, i) in dataSources">
</ul> {{ val.name }}: {{ format(prices[i]) }}
</li>
</ul>
<p>Standard Deviation: {{ stdev }}</p>
</div>
</template> </template>
<style scoped> <style scoped>