From 735389d37c27affa4dcb49384cfe6b4b11e12109 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 26 Oct 2023 08:23:09 -0400 Subject: [PATCH 1/5] added correction of homework 2 --- homework2/evaluation.mjs | 20 ++++++ homework2/index.html | 137 +++++++++++++++++++++++++++++++++++++++ homework2/script.js | 17 +++++ homework2/test.html | 32 +++++++++ 4 files changed, 206 insertions(+) create mode 100644 homework2/evaluation.mjs create mode 100644 homework2/index.html create mode 100644 homework2/script.js create mode 100644 homework2/test.html diff --git a/homework2/evaluation.mjs b/homework2/evaluation.mjs new file mode 100644 index 0000000..ed42629 --- /dev/null +++ b/homework2/evaluation.mjs @@ -0,0 +1,20 @@ +import mocha from "https://esm.sh/mocha@10.2.0" +import {assert} from "https://esm.sh/chai@4.3.10" +mocha.setup('bdd'); + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} +it('should run a bogus test', () => { + assert.equal(1 + 1, 2); +}); + +it("should copy text", async () => { + const expt = "toto"; + document.getElementById("input-1").value = expt; + document.getElementById("button-1").click(); + await sleep(1000); + assert.equal(expt, document.getElementById("result-box-1").innerText) + +}); +mocha.run(); \ No newline at end of file diff --git a/homework2/index.html b/homework2/index.html new file mode 100644 index 0000000..4f7e50c --- /dev/null +++ b/homework2/index.html @@ -0,0 +1,137 @@ + + + + + title + + + + +
+ +
+
+

#main-title

+
+
+ +
+
+
+
+
+ + +
+
+
+ #result-box-1 +
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +
+
+
+
+ #result-box-2 +
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +
+
+
+
+ #result-box-3 +
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +
+
+
+
+ #result-box-4 +
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+ + + + + + + + \ No newline at end of file diff --git a/homework2/script.js b/homework2/script.js new file mode 100644 index 0000000..38df685 --- /dev/null +++ b/homework2/script.js @@ -0,0 +1,17 @@ + + +function exo1b() { + let userInput = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = userInput; +} + +document.getElementById("button-1").onclick = exo1b; + + +function exo1c() { + let userInput = document.getElementById("input-2").value; + + document.getElementById("result-box-3").innerText = getExo1c; +} + +document.getElementById("button-2").onclick = exo1b; \ No newline at end of file diff --git a/homework2/test.html b/homework2/test.html new file mode 100644 index 0000000..8e4727c --- /dev/null +++ b/homework2/test.html @@ -0,0 +1,32 @@ + + + + + Mocha Tests + + + + +
+ + + + + + + + + + + + + \ No newline at end of file From c3945fff829ebd36f3c73496817149064540dba4 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 26 Oct 2023 15:40:45 -0400 Subject: [PATCH 2/5] added correction (to almost all quesitons) --- homework2/evaluation.mjs | 407 ++++++++++++++++++++++++++++++++++++++- homework2/index.html | 2 + homework2/script.js | 196 ++++++++++++++++++- 3 files changed, 590 insertions(+), 15 deletions(-) diff --git a/homework2/evaluation.mjs b/homework2/evaluation.mjs index ed42629..bb9e10c 100644 --- a/homework2/evaluation.mjs +++ b/homework2/evaluation.mjs @@ -5,16 +5,407 @@ mocha.setup('bdd'); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -it('should run a bogus test', () => { - assert.equal(1 + 1, 2); + +describe("question 1", function(){ + it("should have proper title", function(){ + const expt = "Les Bases De La Programmation"; + assert.equal(expt, document.getElementById("main-title").innerText) + }) }); -it("should copy text", async () => { - const expt = "toto"; - document.getElementById("input-1").value = expt; - document.getElementById("button-1").click(); - await sleep(1000); - assert.equal(expt, document.getElementById("result-box-1").innerText) +describe("question 2", function(){ + it("should copy a random text", async function() { + const expt = "toto"; + document.getElementById("input-1").value = expt; + document.getElementById("button-1").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should copy an empty text", async function() { + const expt = ""; + document.getElementById("input-1").value = expt; + document.getElementById("button-1").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); +}); + +describe("question 3", function(){ + it("should mark when input starts with letter a", async function() { + const expt = "input-2 commence par la lettre 'a'"; + document.getElementById("input-2").value = "a la la"; + document.getElementById("button-2").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); + + it("should mark when input does not starts with letter a", async function() { + const expt = "input-2 ne commence pas par la lettre 'a'"; + document.getElementById("input-2").value = "o lo lo"; + document.getElementById("button-2").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); +}); + + +describe("question 4", function(){ + it("should mark when input-2 < input-3", async function() { + const expt = "input-2 < input-3"; + document.getElementById("input-2").value = "allo"; + document.getElementById("input-3").value = "bonjour"; + document.getElementById("button-3").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should mark when input-2 == input-3", async function() { + const expt = "input-2 >= input-3"; + document.getElementById("input-2").value = "allo"; + document.getElementById("input-3").value = "lila"; + document.getElementById("button-3").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should mark when input-2 > input-3", async function() { + const expt = "input-2 >= input-3"; + document.getElementById("input-2").value = "bonjour"; + document.getElementById("input-3").value = "allo"; + document.getElementById("button-3").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); +}); + +describe("question 5", function(){ + it("should mark when all equals", async function() { + const expt = "Tout est pareil"; + document.getElementById("input-1").value = "privet"; + document.getElementById("input-2").value = "privet"; + document.getElementById("input-3").value = "privet"; + document.getElementById("input-4").value = "privet"; + + document.getElementById("button-4").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should mark when not all equals", async function() { + const expt = "Rien n'est pareil"; + document.getElementById("input-1").value = "privet"; + document.getElementById("input-2").value = ""; + document.getElementById("input-3").value = "privet"; + document.getElementById("input-4").value = "privet"; + + document.getElementById("button-4").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); }); + +describe("question 6", function(){ + it("should compute correctly when number", async function() { + const n = 5.5 + const expt = (n - 32) * 1.8; + document.getElementById("input-1").value = n; + + document.getElementById("button-5").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should compute correctly when number (second point)", async function() { + const n = 10 + const expt = (n - 32) * 1.8; + document.getElementById("input-1").value = n; + + document.getElementById("button-5").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should error message when input not a number", async function() { + const expt = "impossible de calculer"; + document.getElementById("input-1").value = "hello"; + document.getElementById("button-5").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should error message when input empty", async function() { + const expt = "impossible de calculer"; + document.getElementById("input-1").value = ""; + document.getElementById("button-5").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + +}); + + +describe("question 7", function(){ + it("should compute correctly when positive number", async function() { + const n = 5.5 + const expt = 5.5; + document.getElementById("input-2").value = n; + + document.getElementById("button-6").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); + + it("should compute correctly when negative number", async function() { + const n = -5.5 + const expt = 5.5; + document.getElementById("input-2").value = n; + + document.getElementById("button-6").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); + + it("should error message when input not a number", async function() { + const expt = "impossible de calculer"; + document.getElementById("input-2").value = "ceci n'est pas un nombre"; + document.getElementById("button-6").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); + + it("should error message when input empty", async function() { + const expt = "impossible de calculer"; + document.getElementById("input-2").value = ""; + document.getElementById("button-6").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + }); +}); + + +describe("question 8", function(){ + + it("should compute correctly when all numbers", async function() { + const expt = 2 * 3 * 4 * 5; + document.getElementById("input-1").value = 2; + document.getElementById("input-2").value = 3; + document.getElementById("input-3").value = 4; + document.getElementById("input-4").value = 5; + + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should compute correctly when some numbers", async function() { + const expt = 2 * 1 * 4 * 1; + document.getElementById("input-1").value = 2; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = 4; + document.getElementById("input-4").value = "eezzz"; + + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should compute correctly when floats", async function() { + const expt = 2.6 * 1 * 4.5 * 1; + document.getElementById("input-1").value = 2.6; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = 4.5; + document.getElementById("input-4").value = "eezzz"; + + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should compute correctly when no values", async function() { + const expt = 1; + document.getElementById("input-1").value = ""; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = ""; + document.getElementById("input-4").value = "eezzz"; + + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); +}); + + +describe("question 9", function(){ + + it("should compute correctly when all numbers", async function() { + const expt = 2 + 3 + 4 + 5; + document.getElementById("input-1").value = 2; + document.getElementById("input-2").value = 3; + document.getElementById("input-3").value = 4; + document.getElementById("input-4").value = 5; + + document.getElementById("button-8").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should compute correctly when some numbers", async function() { + const expt = 2 + 0 + 4 + 0; + document.getElementById("input-1").value = 2; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = 4; + document.getElementById("input-4").value = "eezzz"; + + document.getElementById("button-8").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should compute correctly when floats", async function() { + const expt = 2.6 + 0 + 4.5 + 0; + document.getElementById("input-1").value = 2.6; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = 4.5; + document.getElementById("input-4").value = "eezzz"; + + 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 = ""; + document.getElementById("input-2").value = "gizz"; + document.getElementById("input-3").value = ""; + document.getElementById("input-4").value = "eezzz"; + + document.getElementById("button-8").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); +}); + + +describe("question 10", function(){ + + it("should translate Lapin", async function() { + const expt = 'Kuniklo'; + document.getElementById("input-1").value = 'Lapin'; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should translate Lapinne", async function() { + const expt = 'Kuniklino'; + document.getElementById("input-1").value = 'Lapinne'; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should translate Ville", async function() { + const expt = 'Urbo'; + document.getElementById("input-1").value = 'Ville'; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should translate Téléphone Cellulaire", async function() { + const expt = 'Poŝtelefono'; + document.getElementById("input-1").value = 'Téléphone Cellulaire'; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should not translate empty word", async function() { + const expt = 'mot non trouvé'; + document.getElementById("input-1").value = ''; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + + it("should not translate unknown word", async function() { + const expt = 'mot non trouvé'; + document.getElementById("input-1").value = 'chauve-souris'; + document.getElementById("button-9").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + }); + +}); + + +describe("question 11", function() { + + it("should mark all big numbers", async function () { + const expt = "Est un grand nombre"; + document.getElementById("input-1").value = 2000; + document.getElementById("input-2").value = 2000; + document.getElementById("input-3").value = 2000; + document.getElementById("input-4").value = 2000; + document.getElementById("button-10").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + assert.equal(expt, document.getElementById("result-box-2").innerText) + assert.equal(expt, document.getElementById("result-box-3").innerText) + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should mark small big numbers", async function () { + const expt = "Est un petit nombre"; + document.getElementById("input-1").value = 500; + document.getElementById("input-2").value = 500; + document.getElementById("input-3").value = 500; + document.getElementById("input-4").value = 500; + + document.getElementById("button-10").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-1").innerText) + assert.equal(expt, document.getElementById("result-box-2").innerText) + assert.equal(expt, document.getElementById("result-box-3").innerText) + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should mark all on the threshold", async function () { + const expt = "Est un grand nombre"; + document.getElementById("input-1").value = 1000; + document.getElementById("input-2").value = 1000; + document.getElementById("input-3").value = 1000; + document.getElementById("input-4").value = 1000; + + document.getElementById("button-10").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-1").innerText) + assert.equal(expt, document.getElementById("result-box-2").innerText) + assert.equal(expt, document.getElementById("result-box-3").innerText) + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); + + it("should mark all not number", async function () { + const expt = "N'est pas un nombre"; + document.getElementById("input-1").value = "gratte"; + document.getElementById("input-2").value = "gratte"; + document.getElementById("input-3").value = "gratte"; + document.getElementById("input-4").value = "gratte"; + + document.getElementById("button-10").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-1").innerText) + assert.equal(expt, document.getElementById("result-box-2").innerText) + assert.equal(expt, document.getElementById("result-box-3").innerText) + assert.equal(expt, document.getElementById("result-box-4").innerText) + }); +}); + + mocha.run(); \ No newline at end of file diff --git a/homework2/index.html b/homework2/index.html index 4f7e50c..307f14c 100644 --- a/homework2/index.html +++ b/homework2/index.html @@ -7,6 +7,8 @@ + +
diff --git a/homework2/script.js b/homework2/script.js index 38df685..da2c8ef 100644 --- a/homework2/script.js +++ b/homework2/script.js @@ -1,17 +1,199 @@ +document.getElementById("main-title").innerText = "Les Bases De La Programmation"; + -function exo1b() { +function exo2() { let userInput = document.getElementById("input-1").value; document.getElementById("result-box-1").innerText = userInput; } -document.getElementById("button-1").onclick = exo1b; +document.getElementById("button-1").onclick = exo2; -function exo1c() { - let userInput = document.getElementById("input-2").value; - - document.getElementById("result-box-3").innerText = getExo1c; +function getMessageForExo3(userInput) { + if(userInput.startsWith("a")){ + return "input-2 commence par la lettre 'a'"; + } + return "input-2 ne commence pas par la lettre 'a'"; } -document.getElementById("button-2").onclick = exo1b; \ No newline at end of file +function exo3() { + let userInput = document.getElementById("input-2").value; + document.getElementById("result-box-2").innerText = getMessageForExo3(userInput); +} + +document.getElementById("button-2").onclick = exo3; + + +function getMessageForExo4(userInput2, userInput3) { + if(userInput2.length < userInput3.length){ + return "input-2 < input-3"; + } + return "input-2 >= input-3"; +} + +function exo4() { + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-3").value; + document.getElementById("result-box-3").innerText = getMessageForExo4(userInput2, userInput3); +} + +document.getElementById("button-3").onclick = exo4; + +function allEquals(a, b, c, d){ + return a === b && b === c && c === d +} +function getMessageForExo5(userInput1, userInput2, userInput3, userInput4) { + if(allEquals(userInput1, userInput2, userInput3, userInput4)){ + return "Tout est pareil"; + } + return "Rien n'est pareil"; +} + +function exo5() { + let userInput1 = document.getElementById("input-2").value; + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-2").value; + let userInput4 = document.getElementById("input-3").value; + document.getElementById("result-box-4").innerText = getMessageForExo5(userInput1, userInput2, userInput3, userInput4); +} + +document.getElementById("button-4").onclick = exo5; + +function isNumber(x){ + return x !== "" && !isNaN(x); +} + +function getMessageForExo6(userInput) { + if(!isNumber(userInput)){ + return "impossible de calculer"; + } + return (parseFloat(userInput) -32) * 1.8 +} + +function exo6() { + let userInput = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = getMessageForExo6(userInput); +} + +document.getElementById("button-5").onclick = exo6; + + +function getMessageForExo7(userInput) { + if(!isNumber(userInput)){ + return "impossible de calculer"; + } + return Math.abs(parseFloat(userInput)); +} + +function exo7() { + let userInput = document.getElementById("input-2").value; + document.getElementById("result-box-2").innerText = getMessageForExo7(userInput); +} + +document.getElementById("button-6").onclick = exo7; + +function parseFloatOrDefault(userInput, default_) { + if(isNumber(userInput)){ + return parseFloat(userInput); + } + return default_; +} + +function parseFloatOr1(userInput) { + return parseFloatOrDefault(userInput, 1); +} + +function getMessageForExo8(userInput1, userInput2, userInput3, userInput4) { + return parseFloatOr1(userInput1) + * parseFloatOr1(userInput2) + * parseFloatOr1(userInput3) + * parseFloatOr1(userInput4); +} + +function exo8() { + let userInput1 = document.getElementById("input-1").value; + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-3").value; + let userInput4 = document.getElementById("input-4").value; + document.getElementById("result-box-3").innerText = getMessageForExo8(userInput1, userInput2, userInput3, userInput4); +} + +document.getElementById("button-7").onclick = exo8; + + + +function parseFloatOr0(userInput) { + return parseFloatOrDefault(userInput, 0); +} + +function getMessageForExo9(userInput1, userInput2, userInput3, userInput4) { + return parseFloatOr0(userInput1) + + parseFloatOr0(userInput2) + + parseFloatOr0(userInput3) + + parseFloatOr0(userInput4); +} + +function exo9() { + let userInput1 = document.getElementById("input-1").value; + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-3").value; + let userInput4 = document.getElementById("input-4").value; + document.getElementById("result-box-4").innerText = getMessageForExo9(userInput1, userInput2, userInput3, userInput4); +} + +document.getElementById("button-8").onclick = exo9; + + +function translate(word){ + if(word === 'Lapin'){ + return 'Kuniklo'; + } + if(word === 'Lapinne'){ + return 'Kuniklino'; + } + if(word === 'Téléphone Cellulaire'){ + return 'Poŝtelefono'; + } + if(word === 'Ville'){ + return 'Urbo'; + } + return 'mot non trouvé'; +} + +function exo10() { + let userInput = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = translate(userInput); +} + +document.getElementById("button-9").onclick = exo10; + + + +function numberSizeMessage(userInput){ + if(!isNumber(userInput)){ + return "N'est pas un nombre"; + } + if(parseFloat(userInput) >= 1000){ + return "Est un grand nombre"; + } + return "Est un petit nombre"; +} + +function exo11() { + let userInput1 = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = numberSizeMessage(userInput1); + + let userInput2 = document.getElementById("input-2").value; + document.getElementById("result-box-2").innerText = numberSizeMessage(userInput2); + + let userInput3 = document.getElementById("input-3").value; + document.getElementById("result-box-3").innerText = numberSizeMessage(userInput3); + + let userInput4 = document.getElementById("input-4").value; + document.getElementById("result-box-4").innerText = numberSizeMessage(userInput4); +} + +document.getElementById("button-10").onclick = exo11; + + From 7dcfcf7889d6942689095a4b1100594beb207f64 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 26 Oct 2023 16:20:44 -0400 Subject: [PATCH 3/5] finished correction --- homework2/evaluation.mjs | 174 +++++++++++++++++++++++++++++++++++++++ homework2/script.js | 138 ++++++++++++++++++++++++++----- 2 files changed, 292 insertions(+), 20 deletions(-) diff --git a/homework2/evaluation.mjs b/homework2/evaluation.mjs index bb9e10c..aff86d4 100644 --- a/homework2/evaluation.mjs +++ b/homework2/evaluation.mjs @@ -407,5 +407,179 @@ describe("question 11", function() { }); }); +describe("question 12", function() { + + it("should mark when sorted numbers", async function () { + const expt = "les données sont ordonnées"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = 2; + document.getElementById("input-3").value = 3; + document.getElementById("input-4").value = 4; + + document.getElementById("button-11").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should mark when sorted numbers (2)", async function () { + const expt = "les données sont ordonnées"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = 1; + document.getElementById("input-3").value = 2; + document.getElementById("input-4").value = 2; + + document.getElementById("button-11").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should mark when not a number", async function () { + const expt = "les données ne sont pas ordonnées"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = 1; + document.getElementById("input-3").value = 2; + document.getElementById("input-4").value = "hello"; + + document.getElementById("button-11").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + + it("should mark when no numbers", async function () { + const expt = "les données ne sont pas ordonnées"; + document.getElementById("input-1").value = "a"; + document.getElementById("input-2").value = "b"; + document.getElementById("input-3").value = "c"; + document.getElementById("input-4").value = "e"; + + document.getElementById("button-11").click(); + await sleep(100); + + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + +}); + + +describe("question 13", function() { + + it("should work as expected (1)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = 2; + document.getElementById("input-3").value = 3; + document.getElementById("input-4").value = 4; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(minMessage, document.getElementById("result-box-1").innerText) + assert.equal(noMinOrMaxMessage, document.getElementById("result-box-2").innerText) + assert.equal(noMinOrMaxMessage, document.getElementById("result-box-3").innerText) + assert.equal(maxMessage, document.getElementById("result-box-4").innerText) + }); + + it("should work as expected (2)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = 2; + document.getElementById("input-3").value = 1; + document.getElementById("input-4").value = 2; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(minMessage, document.getElementById("result-box-1").innerText) + assert.equal(maxMessage, document.getElementById("result-box-2").innerText) + assert.equal(minMessage, document.getElementById("result-box-3").innerText) + assert.equal(maxMessage, document.getElementById("result-box-4").innerText) + }); + + it("should work as expected (3)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = 1; + document.getElementById("input-2").value = "ramallah"; + document.getElementById("input-3").value = 1; + document.getElementById("input-4").value = 2; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(minMessage, document.getElementById("result-box-1").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) + assert.equal(minMessage, document.getElementById("result-box-3").innerText) + assert.equal(maxMessage, document.getElementById("result-box-4").innerText) + }); + + it("should work as expected (4)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = ""; + document.getElementById("input-2").value = ""; + document.getElementById("input-3").value = ""; + document.getElementById("input-4").value =""; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(noNumberMessage, document.getElementById("result-box-1").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-3").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-4").innerText) + }); + + it("should work as expected (5)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = 5; + document.getElementById("input-2").value = ""; + document.getElementById("input-3").value = ""; + document.getElementById("input-4").value = 1; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(maxMessage, document.getElementById("result-box-1").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) + assert.equal(noNumberMessage, document.getElementById("result-box-3").innerText) + assert.equal(minMessage, document.getElementById("result-box-4").innerText) + }); + + it("should work as expected (6)", async function () { + const minMessage = "Minima"; + const maxMessage = "Maxima"; + const noMinOrMaxMessage = "Ni minima, ni maxima"; + const noNumberMessage = "Pas un nombre"; + document.getElementById("input-1").value = 5; + document.getElementById("input-2").value = 5; + document.getElementById("input-3").value = 5; + document.getElementById("input-4").value = 5; + + document.getElementById("button-12").click(); + await sleep(100); + + assert.equal(minMessage, document.getElementById("result-box-1").innerText) + assert.equal(minMessage, document.getElementById("result-box-2").innerText) + assert.equal(minMessage, document.getElementById("result-box-3").innerText) + assert.equal(minMessage, document.getElementById("result-box-4").innerText) + }); + +}); mocha.run(); \ No newline at end of file diff --git a/homework2/script.js b/homework2/script.js index da2c8ef..897d1e6 100644 --- a/homework2/script.js +++ b/homework2/script.js @@ -1,4 +1,3 @@ - document.getElementById("main-title").innerText = "Les Bases De La Programmation"; @@ -11,7 +10,7 @@ document.getElementById("button-1").onclick = exo2; function getMessageForExo3(userInput) { - if(userInput.startsWith("a")){ + if (userInput.startsWith("a")) { return "input-2 commence par la lettre 'a'"; } return "input-2 ne commence pas par la lettre 'a'"; @@ -26,7 +25,7 @@ document.getElementById("button-2").onclick = exo3; function getMessageForExo4(userInput2, userInput3) { - if(userInput2.length < userInput3.length){ + if (userInput2.length < userInput3.length) { return "input-2 < input-3"; } return "input-2 >= input-3"; @@ -40,11 +39,12 @@ function exo4() { document.getElementById("button-3").onclick = exo4; -function allEquals(a, b, c, d){ +function allEquals(a, b, c, d) { return a === b && b === c && c === d } + function getMessageForExo5(userInput1, userInput2, userInput3, userInput4) { - if(allEquals(userInput1, userInput2, userInput3, userInput4)){ + if (allEquals(userInput1, userInput2, userInput3, userInput4)) { return "Tout est pareil"; } return "Rien n'est pareil"; @@ -60,15 +60,15 @@ function exo5() { document.getElementById("button-4").onclick = exo5; -function isNumber(x){ +function isNumber(x) { return x !== "" && !isNaN(x); } function getMessageForExo6(userInput) { - if(!isNumber(userInput)){ + if (!isNumber(userInput)) { return "impossible de calculer"; } - return (parseFloat(userInput) -32) * 1.8 + return (parseFloat(userInput) - 32) * 1.8 } function exo6() { @@ -80,7 +80,7 @@ document.getElementById("button-5").onclick = exo6; function getMessageForExo7(userInput) { - if(!isNumber(userInput)){ + if (!isNumber(userInput)) { return "impossible de calculer"; } return Math.abs(parseFloat(userInput)); @@ -94,7 +94,7 @@ function exo7() { document.getElementById("button-6").onclick = exo7; function parseFloatOrDefault(userInput, default_) { - if(isNumber(userInput)){ + if (isNumber(userInput)) { return parseFloat(userInput); } return default_; @@ -122,7 +122,6 @@ function exo8() { document.getElementById("button-7").onclick = exo8; - function parseFloatOr0(userInput) { return parseFloatOrDefault(userInput, 0); } @@ -145,17 +144,17 @@ function exo9() { document.getElementById("button-8").onclick = exo9; -function translate(word){ - if(word === 'Lapin'){ +function translate(word) { + if (word === 'Lapin') { return 'Kuniklo'; } - if(word === 'Lapinne'){ + if (word === 'Lapinne') { return 'Kuniklino'; } - if(word === 'Téléphone Cellulaire'){ + if (word === 'Téléphone Cellulaire') { return 'Poŝtelefono'; } - if(word === 'Ville'){ + if (word === 'Ville') { return 'Urbo'; } return 'mot non trouvé'; @@ -169,12 +168,11 @@ function exo10() { document.getElementById("button-9").onclick = exo10; - -function numberSizeMessage(userInput){ - if(!isNumber(userInput)){ +function numberSizeMessage(userInput) { + if (!isNumber(userInput)) { return "N'est pas un nombre"; } - if(parseFloat(userInput) >= 1000){ + if (parseFloat(userInput) >= 1000) { return "Est un grand nombre"; } return "Est un petit nombre"; @@ -197,3 +195,103 @@ function exo11() { document.getElementById("button-10").onclick = exo11; +function allNumbers(userInput1, userInput2, userInput3, userInput4) { + return isNumber(userInput1) + && isNumber(userInput2) + && isNumber(userInput3) + && isNumber(userInput4); +} + +function areSorted(number, number2, number3, number4) { + return number <= number2 && number2 <= number3 && number3 <= number4; +} + +function getMessageForExo12(userInput1, userInput2, userInput3, userInput4) { + if (!allNumbers(userInput1, userInput2, userInput3, userInput4)) { + return "les données ne sont pas ordonnées"; + } + const areAllSorted = areSorted(parseFloat(userInput1), + parseFloat(userInput2), + parseFloat(userInput3), + parseFloat(userInput4)); + if(!areAllSorted){ + return "les données ne sont pas ordonnées"; + } + return "les données sont ordonnées"; +} + +function exo12() { + let userInput1 = document.getElementById("input-1").value; + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-3").value; + let userInput4 = document.getElementById("input-4").value; + document.getElementById("result-box-3").innerText = getMessageForExo12(userInput1, userInput2, userInput3, userInput4); +} + +document.getElementById("button-11").onclick = exo12; + + +function getMinNumericalValue(userInput1, userInput2, userInput3, userInput4) { + let minValue = 99999999999999; + if(isNumber(userInput1) && parseFloat(userInput1) < minValue){ + minValue = parseFloat(userInput1); + } + if(isNumber(userInput2) && parseFloat(userInput2) < minValue){ + minValue = parseFloat(userInput2); + } + if(isNumber(userInput3) && parseFloat(userInput3) < minValue){ + minValue = parseFloat(userInput3); + } + if(isNumber(userInput4) && parseFloat(userInput4) < minValue){ + minValue = parseFloat(userInput4); + } + return minValue; +} + +function getMaxNumericalValue(userInput1, userInput2, userInput3, userInput4) { + let maxValue = -99999999999999; + if(isNumber(userInput1) && parseFloat(userInput1) > maxValue){ + maxValue = parseFloat(userInput1); + } + if(isNumber(userInput2) && parseFloat(userInput2) > maxValue){ + maxValue = parseFloat(userInput2); + } + if(isNumber(userInput3) && parseFloat(userInput3) > maxValue){ + maxValue = parseFloat(userInput3); + } + if(isNumber(userInput4) && parseFloat(userInput4) > maxValue){ + maxValue = parseFloat(userInput4); + } + return maxValue; +} + +function getMinMaxMessage(userInput, minValue, maxValue){ + if(!isNumber(userInput)){ + return "Pas un nombre"; + } + if(parseFloat(userInput) === minValue){ + return "Minima"; + } + if(parseFloat(userInput) === maxValue){ + return "Maxima"; + } + return "Ni minima, ni maxima"; +} + +function exo13() { + let userInput1 = document.getElementById("input-1").value; + let userInput2 = document.getElementById("input-2").value; + let userInput3 = document.getElementById("input-3").value; + let userInput4 = document.getElementById("input-4").value; + const minValue = getMinNumericalValue(userInput1, userInput2, userInput3, userInput4); + const maxValue = getMaxNumericalValue(userInput1, userInput2, userInput3, userInput4); + + + document.getElementById("result-box-1").innerText = getMinMaxMessage(userInput1, minValue, maxValue); + document.getElementById("result-box-2").innerText = getMinMaxMessage(userInput2, minValue, maxValue); + document.getElementById("result-box-3").innerText = getMinMaxMessage(userInput3, minValue, maxValue); + document.getElementById("result-box-4").innerText = getMinMaxMessage(userInput4, minValue, maxValue); +} + +document.getElementById("button-12").onclick = exo13; + From aabbc74a065fa102aa7392fe2ae2d5b9e252b91c Mon Sep 17 00:00:00 2001 From: didier amyot Date: Fri, 3 Nov 2023 09:37:46 -0400 Subject: [PATCH 4/5] 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() { From 79492e5b62109db0d3444748e154ad63dbe07625 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 6 Nov 2023 07:01:59 -0500 Subject: [PATCH 5/5] fixed last homework --- homework2/evaluation.mjs | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/homework2/evaluation.mjs b/homework2/evaluation.mjs index 6d0f5cf..e4295d4 100644 --- a/homework2/evaluation.mjs +++ b/homework2/evaluation.mjs @@ -223,6 +223,18 @@ describe("question 8", function(){ assert.equal(expt, document.getElementById("result-box-3").innerText) }); + it("should compute correctly with zeros", async function() { + const expt = 2 * 3 * 0 * 5; + document.getElementById("input-1").value = 2; + document.getElementById("input-2").value = 0; + document.getElementById("input-3").value = 3; + document.getElementById("input-4").value = 5; + + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + }); + it("should compute correctly when no values", async function() { const expt = 1; document.getElementById("input-1").value = ""; @@ -530,29 +542,10 @@ describe("question 13", function() { await sleep(100); assert.equal(minMessage, document.getElementById("result-box-1").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) assert.equal(minMessage, document.getElementById("result-box-3").innerText) assert.equal(maxMessage, document.getElementById("result-box-4").innerText) }); - it("should work as expected (4)", async function () { - const minMessage = "Minima"; - const maxMessage = "Maxima"; - const noMinOrMaxMessage = "Ni minima, ni maxima"; - const noNumberMessage = "Pas un nombre"; - document.getElementById("input-1").value = ""; - document.getElementById("input-2").value = ""; - document.getElementById("input-3").value = ""; - document.getElementById("input-4").value =""; - - document.getElementById("button-12").click(); - await sleep(100); - - assert.equal(noNumberMessage, document.getElementById("result-box-1").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-3").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-4").innerText) - }); it("should work as expected (5)", async function () { const minMessage = "Minima"; @@ -568,8 +561,6 @@ describe("question 13", function() { await sleep(100); assert.equal(maxMessage, document.getElementById("result-box-1").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-2").innerText) - assert.equal(noNumberMessage, document.getElementById("result-box-3").innerText) assert.equal(minMessage, document.getElementById("result-box-4").innerText) });