From c9f5fd108265890cdf2278e0340d1b249dc2b88c Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 26 Aug 2024 18:45:07 +1000 Subject: [PATCH] Do not display result if invalid inputs --- index.html | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index 7a7c6ea..67dad83 100644 --- a/index.html +++ b/index.html @@ -58,7 +58,7 @@ -
+
@@ -103,7 +103,7 @@ return phototherapy_38wks(d); } if (gestation < 22) { - throw new Exception('Invalid gestation'); + throw new Error('Invalid gestation'); } if (d <= 0) { return 40; @@ -120,7 +120,7 @@ return exchange_38wks(d); } if (gestation < 22) { - throw new Exception('Invalid gestation'); + throw new Error('Invalid gestation'); } if (d <= 0) { return 80; @@ -137,8 +137,15 @@ function plotGraphData() { let gestation = document.getElementById('gestation').valueAsNumber; - chart.data.datasets[0].data = [...Array(14*24).keys()].map((i) => i / 24).map((d) => ({x: d, y: phototherapy_thresh(d, gestation)})); - chart.data.datasets[1].data = [...Array(14*24).keys()].map((i) => i / 24).map((d) => ({x: d, y: exchange_thresh(d, gestation)})); + + if (isNaN(gestation) || gestation < 22) { + chart.data.datasets[0].data = []; + chart.data.datasets[1].data = []; + } else { + chart.data.datasets[0].data = [...Array(14*24).keys()].map((i) => i / 24).map((d) => ({x: d, y: phototherapy_thresh(d, gestation)})); + chart.data.datasets[1].data = [...Array(14*24).keys()].map((i) => i / 24).map((d) => ({x: d, y: exchange_thresh(d, gestation)})); + } + chart.update(); } @@ -264,30 +271,38 @@ } function updateBilirubin() { + let resultDiv = document.getElementById('result'); + let resultP = resultDiv.querySelector('p'); + + let bilirubin = document.getElementById('bilirubin').valueAsNumber; + let gestation = document.getElementById('gestation').valueAsNumber; + if (document.getElementById('time_birth').valueAsDate === null) { + resultDiv.className = 'hidden'; return; } if (document.getElementById('time_measurement').valueAsDate === null) { + resultDiv.className = 'hidden'; return; } - if (document.getElementById('bilirubin').value === '') { + if (isNaN(bilirubin)) { + resultDiv.className = 'hidden'; + return; + } + if (isNaN(gestation) || gestation < 22) { + resultDiv.className = 'hidden'; return; } // Chronological age in days let d = (document.getElementById('time_measurement').valueAsDate.getTime() - document.getElementById('time_birth').valueAsDate.getTime()) / 1000 / 60 / 60 / 24; - let bilirubin = document.getElementById('bilirubin').valueAsNumber; - let gestation = document.getElementById('gestation').valueAsNumber; let phototherapy_thresh_value = phototherapy_thresh(d, gestation); let exchange_thresh_value = exchange_thresh(d, gestation); chart.data.datasets[2].data = [{x: d, y: bilirubin}]; chart.update(); - let resultDiv = document.getElementById('result'); - let resultP = resultDiv.querySelector('p'); - if (bilirubin < phototherapy_thresh_value) { if (bilirubin < phototherapy_thresh_value - 50) { resultDiv.className = 'border-l-4 border-sky-400 bg-sky-50 py-2 px-4';