Compare commits

..
10 Commits
Author SHA1 Message Date
didier amyot 2034b2f56b Added correction for last homework. 2023-11-16 10:52:18 -05:00
didier amyot e37c3d2f7b added first correction 2023-11-09 10:11:13 -05:00
didier amyot 0ee5fc1190 added alternative answer 2023-11-08 20:48:24 -05:00
didier amyot ac12d7722c added new exercice 2023-11-08 20:24:43 -05:00
didier amyot fe12673e77 bob 2023-11-06 12:51:54 -05:00
didier amyot 924444788e renamed homework 2023-11-06 07:03:41 -05:00
didier amyot 4c57e476b1 Merge branch 'homework2' 2023-11-06 07:03:07 -05:00
didier amyot c795317d38 Update script.js
opsi 2
2023-10-27 17:45:43 -04:00
didier amyot 8894dbde90 Update script.js
opsi
2023-10-27 17:38:05 -04:00
didier amyot f5c0816c94 added solution to petite trempette 2023-10-26 09:14:21 -04:00
12 changed files with 898 additions and 0 deletions

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
function range(n){
let ret = [];
for(let i = 0; i < n; i ++){
ret.push(i)
}
return ret;
}
range(10)
function printEachElement(lst){
for(const elm of lst){
console.log(elm);
}
}
function getElementAt(lst, index){
return lst[index];
}
function getFirstElement(lst){
return lst[0];
}
function getSecondElement(lst){
return lst[1];
}
function restList(lst){
let ret = []
let isFirst = true;
for(const elm of lst){
if(isFirst){
isFirst = false;
} else {
ret.push(elm);
}
}
return ret;
}
function butLast(lst){
let ret = []
let previousElementSet = false;
let previousElement ;
for(const elm of lst){
if(previousElementSet){
ret.push(previousElement);
}
previousElement = elm;
previousElementSet = true;
}
return ret;
}
function dropN(lst, n){
let ret = [];
let i = 0;
for(const elm of lst){
if(i >= n){
ret.push(elm)
}
i ++;
}
return ret;
}
function sumListElements(lst){
let ret = 0;
for(const elm of lst){
ret += elm;
}
return ret;
}
function multiplyListElements(lst){
let ret = 1;
for(const elm of lst){
ret *= elm;
}
return ret;
}
function copy(lst){
let ret = [];
for(const elm of lst){
ret.push(elm)
}
return ret;
}
function isIn(lst, elm){
for(const elm_ of lst){
if(elm_ === elm){
return true;
}
}
return false;
}
function extractPossitiveNumbers(lst){
let ret = [];
for(const elm of lst){
if(elm >= 0){
ret.push(elm);
}
}
return ret;
}
+15
View File
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css" rel="stylesheet">
</head>
<body>
<div id="livre"></div>
<script src="script.js"></script>
</body>
</html>
+51
View File
@@ -0,0 +1,51 @@
console.log("hello world")
// question 1.b
let livre = {
nom: "JS the good part",
year: 2008,
price: 19.75,
publisher: "O reilly"
};
console.log(livre);
function getAttribute(object, key, defaultValue){
if(Object.hasOwn(object, key)){
return object[key];
}
return defaultValue;
}
let key = "age"
let object = {age: 100};
let defaultValue = 0;
console.log("2.a >> " + getAttribute(object, key, defaultValue)) // 100
console.log("2.a >> " + getAttribute({name: "claudette"}, "name", 0)) // claudette
console.log("2.a >> " + getAttribute({age: 100}, "name", "no name")) // no name
console.log("2.a >> " + getAttribute({name: "claudette"}, "age", 0)) // 0
function getAttributeWithLoop(object, targetKey, defaultValue){
for(let key of Object.keys(object)){
let value = object[key];
if(key === targetKey){
return value;
}
}
return defaultValue;
}
console.log("2'.a >> " + getAttributeWithLoop(object, key, defaultValue)) // 100
console.log("2'.a >> " + getAttributeWithLoop({name: "claudette"}, "name", 0)) // claudette
console.log("2'.a >> " + getAttributeWithLoop({age: 100}, "name", "no name")) // no name
console.log("2'.a >> " + getAttributeWithLoop({name: "claudette"}, "age", 0)) // 0
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Petite Trempette</title>
<link href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css" rel="stylesheet">
</head>
<body>
<h1>Petite Trempette</h1>
<script src="script.js"></script>
</body>
</html>
+107
View File
@@ -0,0 +1,107 @@
function add(a, b){
return a + b;
}
alert(add(103, 198));
function multiply(a, b){
return a * b;
}
alert(multiply(31, 42));
function convertFahrenheitToCelcius(f){
return (f - 32) / 1.8;
}
alert(convertFahrenheitToCelcius(100));
function convertCelsiusToKelvin(c){
return c + 274.15;
}
alert(convertCelsiusToKelvin(75));
function convertFahrenheitToKelvin(f){
let c = convertFahrenheitToCelcius(f);
return convertCelsiusToKelvin(c);
}
alert(convertFahrenheitToKelvin(455));
function absolutValue(val){
if(val < 0){
return -1 * val;
}
return val;
}
alert(absolutValue(32));
alert(absolutValue(-76));
function isMinor(age){
if(age < 18){
return "la personne est mineur";
}
if(age < 21){
return "la personne est mineur au états-unis";
}
return "la personne est majeur";
}
alert(isMinor(16));
alert(isMinor(20));
alert(isMinor(56));
function canRetire(age, experienceYear){
if(age + experienceYear >= 80){
return "peut prendre ça retraite";
}
if(age + experienceYear >= 70){
return "presque là";
}
return "nope";
}
alert(canRetire(60, 25));
alert(canRetire(50, 25));
alert(canRetire(40, 20));
function trivialFizzBuzz(n){
if(n < 0){
return "fizz";
}
if(n > 0){
return "buzz";
}
return "fizzBuzz"
}
alert(trivialFizzBuzz(-13));
alert(trivialFizzBuzz(0));
alert(trivialFizzBuzz(52));
function miniFizzBuzz(n){
if(n % 15 === 0){
return "fizzBuzz";
}
if(n % 3 === 0){
return "fizz";
}
if(n % 5 === 0){
return "buzz"
}
return ""
}
alert(miniFizzBuzz(6));
alert(miniFizzBuzz(25));
alert(miniFizzBuzz(45));
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+224
View File
@@ -0,0 +1,224 @@
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 5", function(){
async function testQuestion5(userInput, expt){
document.getElementById("input-4").value = userInput;
document.getElementById("button-4").click();
await sleep(100);
assert.equal(expt, document.getElementById("result-box-4").innerText)
}
it("should find the proper number of existing country is input by user", async function(){
await testQuestion5("Canada", 3);
});
it("should show zero when non existing country is input by user", async function(){
await testQuestion5("France", 0);
});
})
describe("question 6", function(){
async function testQuestion7(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 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 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 city of existing country is input by user", async function(){
await testQuestion7("Canada", "Toronto");
});
it("should show zero when non existing country is input by user", async function(){
await testQuestion7("France", "aucune ville trouvée");
});
})
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 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");
});
})
describe("question 9", function(){
async function testQuestion9(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("Canada", async function(){
await testQuestion9("Canada", 3);
});
it("Mexique", async function(){
await testQuestion9("Mexique", 0);
});
it("France", async function(){
await testQuestion9("France", 0);
});
})
mocha.run();
+121
View File
@@ -0,0 +1,121 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<div class="container">
<div class="columns">
<div class="column">
<h1 class="title is-1" id="main-title">#main-title</h1>
</div>
</div>
<div class="columns">
<div class="column">
<div id="mocha"></div>
</div>
</div>
<div class="columns">
<div class="column">
<div class="notification is-primary" id="result-box-1">
<strong>#result-box-1</strong>
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="column">
<div class="notification is-warning" id="result-box-2">
<strong>#result-box-2</strong>
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="column">
<div class="notification is-primary is-light" id="result-box-3">
<strong>#result-box-3</strong>
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="column">
<div class="notification is-warning is-light" id="result-box-4">
<strong>#result-box-4</strong>
<br>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
</div>
<div class="columns">
<div class="column">
<label>
#input-1
<input class="input" placeholder="Text input" type="text" id="input-1">
</label>
</div>
<div class="column">
<label>
#input-2
<input class="input" placeholder="Text input" type="text" id="input-2">
</label>
</div>
<div class="column">
<label>
#input-3
<input class="input" placeholder="Text input" type="text" id="input-3">
</label>
</div>
<div class="column">
<label>
#input-4
<input class="input" placeholder="Text input" type="text" id="input-4">
</label>
</div>
</div>
<div class="columns">
<div class="column">
<button type="button" class="button is-primary" id="button-1">#button-1</button>
</div>
<div class="column">
<button type="button" class="button is-warning" id="button-2">#button-2</button>
</div>
<div class="column">
<button type="button" class="button is-primary is-light" id="button-3">#button-3</button>
</div>
<div class="column">
<button type="button" class="button is-warning is-light" id="button-4">#button-4</button>
</div>
</div>
<div class="columns">
<div class="column">
<button type="button" class="button is-primary" id="button-5">#button-5</button>
</div>
<div class="column">
<button type="button" class="button is-warning" id="button-6">#button-6</button>
</div>
<div class="column">
<button type="button" class="button is-primary is-light" id="button-7">#button-7</button>
</div>
<div class="column">
<button type="button" class="button is-warning is-light" id="button-8">#button-8</button>
</div>
</div>
</div>
<script src="script.js" type="module"></script>
<script src="evaluation.mjs" type="module"></script>
</body>
</html>
+254
View File
@@ -0,0 +1,254 @@
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 exo5(){
const userInput = document.getElementById("input-4").value;
document.getElementById("result-box-4").innerText = findNumberOfCitiesInCountry(userInput);
}
document.getElementById("button-4").onclick = exo5;
function findPopulationOfCitiesInCountry(countryName) {
let population = 0;
for(let city of cities){
if(city.country === countryName){
population = population + city.population;
}
}
return population;
}
function exo6(){
const userInput = document.getElementById("input-1").value;
document.getElementById("result-box-1").innerText = findPopulationOfCitiesInCountry(userInput);
}
document.getElementById("button-5").onclick = exo6;
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 exo7(){
const userInput = document.getElementById("input-2").value;
document.getElementById("result-box-2").innerText = findLargestCitiesInCountry(userInput);
}
document.getElementById("button-6").onclick = exo7;
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 exo8(){
const userInput = document.getElementById("input-3").value;
document.getElementById("result-box-3").innerText = findCountryOfOrigin(userInput);
}
document.getElementById("button-7").onclick = exo8;
function getPersonsByCityName(cityName) {
const ret = [];
for(const person of persons){
if(person.city === cityName){
ret.push(person);
}
}
return ret;
}
function findAllPeopleInCountryByName(countryName) {
let ret = [];
for(const city of cities){
if(city.country === countryName){
ret = ret.concat(getPersonsByCityName(city.name));
}
}
return ret;
}
function exo9(){
const userInput = document.getElementById("input-4").value;
document.getElementById("result-box-4").innerText = findAllPeopleInCountryByName(userInput).length;
}
document.getElementById("button-8").onclick = exo9;