From aabbc74a065fa102aa7392fe2ae2d5b9e252b91c Mon Sep 17 00:00:00 2001 From: didier amyot Date: Fri, 3 Nov 2023 09:37:46 -0400 Subject: [PATCH] fixed bug --- homework2/evaluation.mjs | 12 ++++++++++++ homework2/script.js | 15 +++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/homework2/evaluation.mjs b/homework2/evaluation.mjs index aff86d4..6d0f5cf 100644 --- a/homework2/evaluation.mjs +++ b/homework2/evaluation.mjs @@ -275,6 +275,18 @@ describe("question 9", function(){ assert.equal(expt, document.getElementById("result-box-4").innerText) }); + it("should remove negative numbers when computing the sum", async function() { + const expt = 2.6 + 0 + 4.5 + 0; + document.getElementById("input-1").value = 2.6; + document.getElementById("input-2").value = -90; + document.getElementById("input-3").value = 4.5; + document.getElementById("input-4").value = -7; + + document.getElementById("button-8").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + it("should compute correctly when no values", async function() { const expt = 0; document.getElementById("input-1").value = ""; diff --git a/homework2/script.js b/homework2/script.js index 897d1e6..9c237c4 100644 --- a/homework2/script.js +++ b/homework2/script.js @@ -126,11 +126,18 @@ function parseFloatOr0(userInput) { return parseFloatOrDefault(userInput, 0); } +function valueOrZeroIfNegative(value){ + if(value < 0){ + return 0; + } + return value; +} + function getMessageForExo9(userInput1, userInput2, userInput3, userInput4) { - return parseFloatOr0(userInput1) - + parseFloatOr0(userInput2) - + parseFloatOr0(userInput3) - + parseFloatOr0(userInput4); + return valueOrZeroIfNegative(parseFloatOr0(userInput1)) + + valueOrZeroIfNegative(parseFloatOr0(userInput2)) + + valueOrZeroIfNegative(parseFloatOr0(userInput3)) + + valueOrZeroIfNegative(parseFloatOr0(userInput4)); } function exo9() {