From facb94432017c6d2247e89eefca444bafeca625d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ku=C4=87mierz?= Date: Thu, 7 Dec 2023 05:25:45 +0100 Subject: [PATCH] add stdev; toggle sources --- src/components/HelloWorld.vue | 44 ++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/components/HelloWorld.vue b/src/components/HelloWorld.vue index eea7d65..f4e4c72 100644 --- a/src/components/HelloWorld.vue +++ b/src/components/HelloWorld.vue @@ -53,9 +53,28 @@ const get = async (url) => { return await response.json(); }; +const filterPrices = () => { + return prices.value.filter(val => { + return typeof val === 'number'; + }); +}; + const calcAverage = () => { - const arr = prices.value; - average.value = arr.reduce((sum, val) => sum + val, 0) / arr.length; + const prices = filterPrices(); + 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 => { @@ -64,6 +83,8 @@ const format = price => { const average = ref(); const prices = ref([]); +const stdev = ref(); +const showSources = ref(true); (() => { let idx = -1; @@ -72,8 +93,9 @@ const prices = ref([]); idx = (idx + 1) % dataSources.length; try { const source = dataSources[idx]; + prices.value[idx] = null; prices.value[idx] = source.pick(await get(source.url)); - calcAverage(); + recalc(); } catch (e) {}; }; loop(); @@ -88,12 +110,16 @@ const prices = ref([]);