From e37c3d2f7b99bee23731860031bef795e1e67f27 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 9 Nov 2023 10:11:13 -0500 Subject: [PATCH] added first correction --- travail-pratique-3/evaluation.mjs | 202 +++++++++++++++++++++++++++ travail-pratique-3/index.html | 135 ++++++++++++++++++ travail-pratique-3/script.js | 224 ++++++++++++++++++++++++++++++ 3 files changed, 561 insertions(+) create mode 100644 travail-pratique-3/evaluation.mjs create mode 100644 travail-pratique-3/index.html create mode 100644 travail-pratique-3/script.js diff --git a/travail-pratique-3/evaluation.mjs b/travail-pratique-3/evaluation.mjs new file mode 100644 index 0000000..bceb403 --- /dev/null +++ b/travail-pratique-3/evaluation.mjs @@ -0,0 +1,202 @@ +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)); +} + +describe("question 1", function(){ + it("should have proper title", function(){ + const expt = "Travail Pratique III"; + assert.equal(expt, document.getElementById("main-title").innerText) + }) +}); + +describe("question 2", function(){ + + async function testQuestion2(userInput, expt){ + document.getElementById("input-1").value = userInput; + document.getElementById("button-1").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + } + + it("should sum up to 3", async function(){ + const userInput = (3) + ""; + const expt = (1 + 2 + 3) + ""; + await testQuestion2(userInput, expt); + }) + + it("should sum up to 10", async function(){ + const userInput = (10) + ""; + const expt = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) + ""; + await testQuestion2(userInput, expt); + }) + + it("should work on zero", async function(){ + const userInput = "0"; + const expt = "0"; + await testQuestion2(userInput, expt); + }) + + it("should not work on random string", async function(){ + const userInput = "allo"; + const expt = "Entré invalide"; + await testQuestion2(userInput, expt); + }) + + it("should not work on negative values", async function(){ + const userInput = "-12"; + const expt = "Entré invalide"; + await testQuestion2(userInput, expt); + }) +}); + +describe("question 3", function(){ + + async function testQuestion3(stepUserInput, limitUserInput, expt){ + document.getElementById("input-1").value = stepUserInput; + document.getElementById("input-2").value = limitUserInput; + document.getElementById("button-2").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + } + + it("should sum up to 3", async function(){ + const stepUserInput = "1" + const limitUserInput = "3"; + const expt = (1 + 2 + 3) + ""; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + + it("should sum up to 10 by step of 2", async function(){ + const stepUserInput = "2" + const limitUserInput = "10"; + const expt = (2 + 4 + 6 + 8 + 10) + ""; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + + it("should not work with step as a string", async function(){ + const stepUserInput = "allo" + const limitUserInput = "10"; + const expt = "Entré invalide"; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + + it("should not work with negative step", async function(){ + const stepUserInput = "-2" + const limitUserInput = "10"; + const expt = "Entré invalide"; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + + it("should not work with limit as a string", async function(){ + const stepUserInput = "2" + const limitUserInput = "allo"; + const expt = "Entré invalide"; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + + it("should not work with negative limit", async function(){ + const stepUserInput = "2" + const limitUserInput = "-10"; + const expt = "Entré invalide"; + await testQuestion3(stepUserInput, limitUserInput, expt); + }) + +}); + + +describe("question 4", function(){ + async function testQuestion4(userInput, expt){ + document.getElementById("input-3").value = userInput; + document.getElementById("button-3").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + } + + it("should find the proper country of an existing city", async function(){ + await testQuestion4("Oran", "Algerie"); + }); + + + it("should show error message when city not found in data", async function(){ + await testQuestion4("Paris", "ville non trouvée"); + }); + +}) + +describe("question 6", function(){ + async function testQuestion6(userInput, expt){ + document.getElementById("input-1").value = userInput; + document.getElementById("button-5").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-1").innerText) + } + + it("should find the proper number of existing country is input by user", async function(){ + await testQuestion6("Canada", 3); + }); + + it("should show zero when non existing country is input by user", async function(){ + await testQuestion6("France", 0); + }); + +}) + + +describe("question 7", function(){ + async function testQuestion7(userInput, expt){ + document.getElementById("input-2").value = userInput; + document.getElementById("button-6").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-2").innerText) + } + + it("should find the proper population of existing country is input by user", async function(){ + await testQuestion7("Canada", 1762949 + 2794356 + 662248); + }); + + it("should show zero when non existing country is input by user", async function(){ + await testQuestion7("France", 0); + }); +}) + + +describe("question 8", function(){ + async function testQuestion8(userInput, expt){ + document.getElementById("input-3").value = userInput; + document.getElementById("button-7").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-3").innerText) + } + + it("should find the proper city of existing country is input by user", async function(){ + await testQuestion8("Canada", "Toronto"); + }); + + it("should show zero when non existing country is input by user", async function(){ + await testQuestion8("France", "aucune ville trouvée"); + }); +}) + +describe("question 9", function(){ + async function testQuestion8(userInput, expt){ + document.getElementById("input-4").value = userInput; + document.getElementById("button-8").click(); + await sleep(100); + assert.equal(expt, document.getElementById("result-box-4").innerText) + } + + it("should find the proper country of existing person entered by input by user", async function(){ + await testQuestion8("Djaffar Bensetti", "Algerie"); + }); + + it("should show error message when person not found", async function(){ + await testQuestion8("Marie Antoinette", "aucune personne trouvée"); + }); +}) + + +mocha.run(); \ No newline at end of file diff --git a/travail-pratique-3/index.html b/travail-pratique-3/index.html new file mode 100644 index 0000000..bbc98f2 --- /dev/null +++ b/travail-pratique-3/index.html @@ -0,0 +1,135 @@ + + + + + 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/travail-pratique-3/script.js b/travail-pratique-3/script.js new file mode 100644 index 0000000..0bc27ee --- /dev/null +++ b/travail-pratique-3/script.js @@ -0,0 +1,224 @@ +document.getElementById("main-title").innerText = "Travail Pratique III"; + + +const cities = [ + {"name": "Montreal", "population": 1762949, "country": "Canada"}, + {"name": "Toronto", "population": 2794356, "country": "Canada"}, + {"name": "Vancouver", "population": 662248, "country": "Canada"}, + + + {"name": "Bogota", "population": 8034649, "country": "Colombie"}, + {"name": "Medellin", "population": 8034649, "country": "Colombie"}, + + + {"name": "Alger", "population": 3154792, "country": "Algerie"}, + {"name": "Oran", "population": 803329, "country": "Algerie"}, + + {"name": "Mexico", "population": 9209944, "country": "Mexique"} + +]; + +const persons = [ + {"name": "Maurice Vachon", "city": "Montreal"}, + {"name": "Jane Munro", "city": "Vancouver"}, + {"name": "Eckhart Tolle", "city": "Vancouver"}, + {"name": "Ingrid Betancourt", "city": "Bogota"}, + {"name": "DJ Pope", "city": "Medellin"}, + {"name": "Djaffar Bensetti", "city": "Oran"} + +]; + +function isNumber(x) { + return x !== "" && !isNaN(x); +} + +function isPositiveInteger(text) { + return isNumber(text) && parseInt(text) >= 0; +} + +function isStrictlyPositiveInteger(text){ + return isPositiveInteger(text) && parseInt(text) !== 0; +} + +function sumUpTo(n){ + let sum = 0; + for(let i = 0; i <= n; i ++){ + sum = sum + i; + } + return sum; +} + +function sumUpToByStep(step, limit){ + let sum = 0; + for(let i = 0; i <= limit; i = i + step){ + sum = sum + i; + } + return sum; + +} + +function exo2Message(userInput){ + if(!isPositiveInteger(userInput)){ + return "Entré invalide"; + } + return sumUpTo(parseInt(userInput)); +} + +function exo2(){ + const userInput = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = exo2Message(userInput); +} + + +document.getElementById("button-1").onclick = exo2; + +function exo3Message(stepUserInput, limitUserInput){ + if(!isStrictlyPositiveInteger(stepUserInput)){ + return "Entré invalide"; + } + if(!isStrictlyPositiveInteger(limitUserInput)){ + return "Entré invalide"; + } + return sumUpToByStep(parseInt(stepUserInput), parseInt(limitUserInput)); +} + +function exo3(){ + const stepUserInput = document.getElementById("input-1").value; + const limitUserInput = document.getElementById("input-2").value; + document.getElementById("result-box-2").innerText = exo3Message(stepUserInput, limitUserInput); +} + + +document.getElementById("button-2").onclick = exo3; + + +function exo4Message(city) { + return getCityByName(city)?.country??"ville non trouvée"; +} + +function exo4(){ + const userInput = document.getElementById("input-3").value; + document.getElementById("result-box-3").innerText = exo4Message(userInput); +} + + +document.getElementById("button-3").onclick = exo4; + + + +function findNumberOfCitiesInCountry(countryName) { + let n = 0; + for(let city of cities){ + if(city.country === countryName){ + n = n + 1; + } + } + return n; +} + +function exo6(){ + const userInput = document.getElementById("input-1").value; + document.getElementById("result-box-1").innerText = findNumberOfCitiesInCountry(userInput); +} + + +document.getElementById("button-5").onclick = exo6; + + +function findPopulationOfCitiesInCountry(countryName) { + let population = 0; + for(let city of cities){ + if(city.country === countryName){ + population = population + city.population; + } + } + return population; +} + +function exo7(){ + const userInput = document.getElementById("input-2").value; + document.getElementById("result-box-2").innerText = findPopulationOfCitiesInCountry(userInput); +} + + +document.getElementById("button-6").onclick = exo7; + +function getCitiesInCountry(country) { + const ret = []; + for(const city of cities){ + if(city.country == country){ + ret.push(city); + } + + } + return ret; +} + +function getLargestCity(cities){ + let largestCity; + let largestCityPopulation = -1; + for(const city of cities){ + if(city.population > largestCityPopulation){ + largestCity = city; + largestCityPopulation = city.population; + } + } + return largestCity; + +} + +function findLargestCitiesInCountry(country){ + const citiesInCountry = getCitiesInCountry(country); + if(citiesInCountry.length === 0){ + return "aucune ville trouvée"; + } + const largestCity = getLargestCity(citiesInCountry); + return largestCity.name; + +} + +function exo8(){ + const userInput = document.getElementById("input-3").value; + document.getElementById("result-box-3").innerText = findLargestCitiesInCountry(userInput); +} + + +document.getElementById("button-7").onclick = exo8; + + +function getPersonByName(personName) { + for(const person of persons){ + if(personName === person.name){ + return person; + } + } +} + +function getCityByName(cityName) { + for(const city of cities){ + if(city.name === cityName){ + return city; + } + } + return {}; +} + +function findCountryOfOrigin(personName){ + const person = getPersonByName(personName); + if(!person){ + return "aucune personne trouvée"; + } + return getCityByName(person.city).country +} + + +function exo9(){ + const userInput = document.getElementById("input-4").value; + document.getElementById("result-box-4").innerText = findCountryOfOrigin(userInput); +} + + +document.getElementById("button-8").onclick = exo9; + + +