diff --git a/qualdev/TD_Debug/deb1.ts b/qualdev/TD_Debug/deb1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cbee8733307928fd256d93ff43bb1647380419e8 --- /dev/null +++ b/qualdev/TD_Debug/deb1.ts @@ -0,0 +1,13 @@ +function deb1() : number{ + let j = 200; + for (let i = 0; i < 10; i++){ + j -= 10; + } + return j; +} + +deb1(); + + + + diff --git a/qualdev/TD_Debug/deb2.ts b/qualdev/TD_Debug/deb2.ts new file mode 100644 index 0000000000000000000000000000000000000000..857b2a76cdb91e975fcbe806dc303d1954cd064a --- /dev/null +++ b/qualdev/TD_Debug/deb2.ts @@ -0,0 +1,11 @@ +function deb2(): number[] { + let j = 200; + let tab: number[] = []; + for (let i = 0; i < 10; i++) { + j -= 10; + tab[i] = j; + } + return tab; +} + +console.log(deb2()); \ No newline at end of file diff --git a/qualdev/TD_Debug/deb3.ts b/qualdev/TD_Debug/deb3.ts new file mode 100644 index 0000000000000000000000000000000000000000..2b2c3d7e97b26f23baf5b773ea32aa7a02b01490 --- /dev/null +++ b/qualdev/TD_Debug/deb3.ts @@ -0,0 +1,11 @@ +function deb2(x: number, y: number): void { + const tmp = y; + y = x; + x = tmp; + console.log(x, y); +} + +let x = 10; +let y = 20; +deb2(x, y); +console.log(x, y); \ No newline at end of file diff --git a/qualdev/TD_Debug/deb4.ts b/qualdev/TD_Debug/deb4.ts new file mode 100644 index 0000000000000000000000000000000000000000..3807df9aa905a88d8852380a64d50d7ac24082b3 --- /dev/null +++ b/qualdev/TD_Debug/deb4.ts @@ -0,0 +1,10 @@ +function deb3(xy: number[]): void { + let tmp = xy[0]; + xy[0] = xy[1]; + xy[1] = tmp; + console.log(xy); +} + +let xy = [10, 20]; +deb3(xy); +console.log(xy); \ No newline at end of file diff --git a/qualdev/TD_Debug/deb5.ts b/qualdev/TD_Debug/deb5.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4e9d2601202c79cecc81382b134467bb7da0eaa --- /dev/null +++ b/qualdev/TD_Debug/deb5.ts @@ -0,0 +1,12 @@ +let j = 10; +deb4(); +console.log(j); + +function deb4(): number { + let j = 25; + for (let i = 0; i < 10; i++) { + let j = 2 * i; + console.log(j); + } + return j; +} \ No newline at end of file diff --git a/qualdev/TD_Debug/deb6.ts b/qualdev/TD_Debug/deb6.ts new file mode 100644 index 0000000000000000000000000000000000000000..242fa9662f31f2855d75d86f363ba9dc488831e5 --- /dev/null +++ b/qualdev/TD_Debug/deb6.ts @@ -0,0 +1,10 @@ +let tab1 = [1, 2, 3]; +let tab2 = tab1; + +tab1[1] = 5; +console.log(tab1, tab2); + +// Copie par clonage +let tab3 = [...tab1]; +tab1[1] = 10; +console.log(tab1, tab3); \ No newline at end of file diff --git a/qualdev/TD_Debug/deb7.ts b/qualdev/TD_Debug/deb7.ts new file mode 100644 index 0000000000000000000000000000000000000000..30bf57814bd57248b7e66d19745263735a9128b7 --- /dev/null +++ b/qualdev/TD_Debug/deb7.ts @@ -0,0 +1,3 @@ +const obj = { x: 1, y: 2 }; +obj.x = 5; +console.log(obj); \ No newline at end of file diff --git a/qualdev/TD_Debug/deb8.ts b/qualdev/TD_Debug/deb8.ts new file mode 100644 index 0000000000000000000000000000000000000000..dbe659070fb01a8203a9d4516e1738c63d1b0693 --- /dev/null +++ b/qualdev/TD_Debug/deb8.ts @@ -0,0 +1,11 @@ +function search(arr: number[], target: number): boolean { + for (let i = 0; i < arr.length; i++) { + if (arr[i] === target) { + return true; + } + } + return false; +} + +console.log(search([1, 2, 3, 4, 5], 3)); +console.log(search([1, 2, 3, 4, 5], 6)); \ No newline at end of file diff --git a/qualdev/TD_EX/Date.html b/qualdev/TD_EX/Date.html new file mode 100644 index 0000000000000000000000000000000000000000..2ba726565a58340b9e9d611b5aaf074943255d28 --- /dev/null +++ b/qualdev/TD_EX/Date.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Test Date</title> +</head> +<body> + <script src="exo4.js"></script> +</body> +</html> diff --git a/qualdev/TD_EX/Errorexo4.ts b/qualdev/TD_EX/Errorexo4.ts new file mode 100644 index 0000000000000000000000000000000000000000..f588d06c84ba5f58c05d19b5678f75165dc97f14 --- /dev/null +++ b/qualdev/TD_EX/Errorexo4.ts @@ -0,0 +1,21 @@ +class Errornumber extends Error{ + constructeur (nombre: string){ + //super(nombre) + this.name = "Errornumber" + } +} + +class Errorindice extends Error{ + constructor (indice: string){ + super(indice) + this.name = "Errorindice" + } +} + + +class Erroroperateur extends Error{ + constructor (operateur: string){ + super(operateur) + this.name = "Erroroperateur"; + } +} diff --git a/qualdev/TD_EX/INE.html b/qualdev/TD_EX/INE.html new file mode 100644 index 0000000000000000000000000000000000000000..35f6466010a7fe2ba6e2644268903f3be0740862 --- /dev/null +++ b/qualdev/TD_EX/INE.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html lang="fr"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Test Date</title> +</head> +<body> + <script src="TD_Exceptions3.js"></script> +</body> +</html> diff --git a/qualdev/TD_EX/INE.js b/qualdev/TD_EX/INE.js new file mode 100644 index 0000000000000000000000000000000000000000..fc7baebf3cf1274d83e17c46908c77d2516a041d --- /dev/null +++ b/qualdev/TD_EX/INE.js @@ -0,0 +1,94 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { + if (ar || !(i in from)) { + if (!ar) ar = Array.prototype.slice.call(from, 0, i); + ar[i] = from[i]; + } + } + return to.concat(ar || Array.prototype.slice.call(from)); +}; +var INEError = /** @class */ (function (_super) { + __extends(INEError, _super); + function INEError(message) { + var _this = _super.call(this, message) || this; + _this.name = "INEError"; + return _this; + } + return INEError; +}(Error)); +function isLetter(c) { + return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z"); +} +function isNumber(c) { + return c >= "0" && c <= "9"; +} +var Etudiant = /** @class */ (function () { + function Etudiant(nom, prenom, ine) { + this.nom = nom; + this.prenom = prenom; + this.setINE(ine); + } + Object.defineProperty(Etudiant.prototype, "ine", { + get: function () { + return this._ine; + }, + enumerable: false, + configurable: true + }); + Etudiant.prototype.setINE = function (ine) { + if (!this.validerINE(ine)) { + throw new INEError("Format incorrect"); + } + this._ine = ine; + }; + Etudiant.prototype.validerINE = function (ine) { + if (ine.length !== 11) { + throw new INEError("Format incorrect"); + } + if (ine.length === 11 && !isLetter(ine[9])) { + throw new INEError("Format incorrect"); + } + for (var i = 0; i < 9; i++) { + if (!isNumber(ine[i])) { + throw new INEError("Format incorrect"); + } + } + return true; + }; + Etudiant.prototype.estChiffre = function (str) { + return __spreadArray([], str, true).every(function (c) { return c >= "0" && c <= "9"; }); + }; + return Etudiant; +}()); +function demanderINE() { + var ine = prompt("Entrez un INE valide (10 chiffres + 1 lettre ou 9 chiffres + 2 lettres) :") || ""; + try { + var etudiant = new Etudiant("Bayiha Mbarga", "Dieudonné", ine); + console.log("INE enregistré avec succès :", etudiant.ine); + } + catch (error) { + if (error instanceof INEError) { + console.log(error.message); + demanderINE(); // + } + else { + console.log("Une erreur inattendue s'est produite."); + } + } +} +demanderINE(); diff --git a/qualdev/TD_EX/INE.ts b/qualdev/TD_EX/INE.ts new file mode 100644 index 0000000000000000000000000000000000000000..3adaf89085531dad14026915c063d5ab5039ac02 --- /dev/null +++ b/qualdev/TD_EX/INE.ts @@ -0,0 +1,77 @@ +class INEError extends Error { + constructor(message: string) { + super(message); + this.name = "INEError"; + } +} + +function isLetter(c: string): boolean { + return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z"); +} +function isNumber(c: string): boolean { + return c >= "0" && c <= "9"; +} + +class Etudiant { + nom: string; + prenom: string; + private _ine: string; + + constructor(nom: string, prenom: string, ine: string) { + this.nom = nom; + this.prenom = prenom; + this.setINE(ine); + } + + get ine(): string { + return this._ine; + } + + setINE(ine: string): void { + if (!this.validerINE(ine)) { + throw new INEError("Format incorrect"); + } + this._ine = ine; + } + + private validerINE(ine: string): boolean { + if (ine.length !== 11) { + throw new INEError("Format incorrect"); + } + if (ine.length === 11 && !isLetter(ine[9])) { + throw new INEError("Format incorrect"); } + + for (let i = 0; i < 9; i++) { + if (!isNumber(ine[i])) { + throw new INEError("Format incorrect"); + } + + } + return true; + } + + private estChiffre(str: string): boolean { + return [...str].every(c => c >= "0" && c <= "9"); + } +} + +function demanderINE(): void { + var ine = prompt("Entrez un INE valide (10 chiffres + 1 lettre ou 9 chiffres + 2 lettres) :") || ""; + try { + var etudiant = new Etudiant("Bayiha Mbarga", "Dieudonné", ine); + console.log("INE enregistré avec succès :", etudiant.ine); + } + catch (error) { + if (error instanceof INEError) { + console.log(error.message); + demanderINE(); // + } + else { + console.log("Une erreur inattendue s'est produite."); + } + } +} +demanderINE(); + + + diff --git a/qualdev/TD_EX/exo1.js b/qualdev/TD_EX/exo1.js new file mode 100644 index 0000000000000000000000000000000000000000..2cf2b5b427d61460707dda4b4a2da5969d09187d --- /dev/null +++ b/qualdev/TD_EX/exo1.js @@ -0,0 +1,94 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var NomVideError = /** @class */ (function (_super) { + __extends(NomVideError, _super); + function NomVideError(message) { + var _this = _super.call(this, message) || this; + _this.name = "NomVideError"; + return _this; + } + return NomVideError; +}(Error)); +var PrenomVideError = /** @class */ (function (_super) { + __extends(PrenomVideError, _super); + function PrenomVideError(message) { + var _this = _super.call(this, message) || this; + _this.name = "PrenomVideError"; + return _this; + } + return PrenomVideError; +}(Error)); +var NomEtPrenomVidesError = /** @class */ (function (_super) { + __extends(NomEtPrenomVidesError, _super); + function NomEtPrenomVidesError(message) { + var _this = _super.call(this, message) || this; + _this.name = "NomEtPrenomVidesError"; + return _this; + } + return NomEtPrenomVidesError; +}(Error)); +function creeInitiales(nom, prenom) { + if (!nom && !prenom) { + throw new NomEtPrenomVidesError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides."); + } + if (!nom) { + throw new NomVideError("Le paramètre 'nom' ne doit pas être vides."); + } + if (!prenom) { + throw new PrenomVideError("Le paramètre'prenom' ne doit pas être vides."); + } + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} +try { + console.log(creeInitiales("Bayiha", "Dieudonne")); + console.log(creeInitiales("Bayiha", "")); +} +catch (error) { + console.error(error.message); +} +function creeInitiales2(nom, prenom) { + if (!nom && !prenom) { + throw new NomEtPrenomVidesError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides."); + } + if (!nom) { + throw new NomVideError("Le paramètre 'nom' ne doit pas être vide."); + } + if (!prenom) { + throw new PrenomVideError("Le paramètre 'prenom' ne doit pas être vide."); + } + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} +// Programme principal +try { + console.log(creeInitiales2("", "")); // Lève NomEtPrenomVidesError +} +catch (error) { + if (error instanceof NomEtPrenomVidesError) { + console.error("Erreur: ".concat(error.message)); + } + else if (error instanceof NomVideError) { + console.error("Erreur: ".concat(error.message)); + } + else if (error instanceof PrenomVideError) { + console.error("Erreur: ".concat(error.message)); + } + else { + console.error("Une erreur inattendue est survenue."); + } +} +finally { + console.log("Fin du programme"); +} diff --git a/qualdev/TD_EX/exo1.ts b/qualdev/TD_EX/exo1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c32edc54464537db26e6e8bfd776c9d34464b19 --- /dev/null +++ b/qualdev/TD_EX/exo1.ts @@ -0,0 +1,44 @@ +class NomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "NomVideError"; + } +} +class PrenomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "PrenomVideError"; + } +} + +class NomEtPrenomVidesError extends Error { + constructor(message: string) { + super(message); + this.name = "NomEtPrenomVidesError"; + } +} + + +function creeInitiales(nom: string, prenom: string): string { + if(!nom && !prenom){ + throw new NomEtPrenomVidesError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides.");} + if (!nom){ + throw new NomVideError("Le paramètre 'nom' ne doit pas être vides.");} + if (!prenom){ + throw new PrenomVideError("Le paramètre'prenom' ne doit pas être vides.");} + + + + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +try { + console.log(creeInitiales("Bayiha", "Dieudonne")); + console.log(creeInitiales("Bayiha", "")); +} catch (error) { + console.error(error.message); +} + + + diff --git a/qualdev/TD_EX/exo2.js b/qualdev/TD_EX/exo2.js new file mode 100644 index 0000000000000000000000000000000000000000..3c1f7899f4cfb6123f2879300c7a0c6e4652ad03 --- /dev/null +++ b/qualdev/TD_EX/exo2.js @@ -0,0 +1,51 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var ValidationError = /** @class */ (function (_super) { + __extends(ValidationError, _super); + function ValidationError(message, typeErreur) { + var _this = _super.call(this, message) || this; + _this.name = "ValidationError"; + _this.typeErreur = typeErreur; + return _this; + } + return ValidationError; +}(Error)); +function creeInitiales2(nom, prenom) { + if (!nom && !prenom) { + throw new ValidationError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides.", "NomEtPrenomVides"); + } + if (!nom) { + throw new ValidationError("Le paramètre 'nom' ne doit pas être vide.", "NomVide"); + } + if (!prenom) { + throw new ValidationError("Le paramètre 'prenom' ne doit pas être vide.", "PrenomVide"); + } + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} +try { + console.log(creeInitiales2("", "")); +} +catch (error) { + if (error instanceof ValidationError) { + console.error("Erreur (".concat(error.typeErreur, "): ").concat(error.message)); + } + else { + console.error("Une erreur inattendue est survenue."); + } +} +finally { + console.log("Fin du programme"); +} diff --git a/qualdev/TD_EX/exo2.ts b/qualdev/TD_EX/exo2.ts new file mode 100644 index 0000000000000000000000000000000000000000..25308ae991e76a352998192f60adf7a5f77902e7 --- /dev/null +++ b/qualdev/TD_EX/exo2.ts @@ -0,0 +1,37 @@ + +class ValidationError extends Error { + typeErreur: string; + + constructor(message: string, typeErreur: string) { + super(message); + this.name = "ValidationError"; + this.typeErreur = typeErreur; + } +} + +function creeInitiales2(nom: string, prenom: string): string { + if (!nom && !prenom) { + throw new ValidationError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides.", "NomEtPrenomVides"); + } + if (!nom) { + throw new ValidationError("Le paramètre 'nom' ne doit pas être vide.", "NomVide"); + } + if (!prenom) { + throw new ValidationError("Le paramètre 'prenom' ne doit pas être vide.", "PrenomVide"); + } + + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +try { + console.log(creeInitiales2("", "")); +} catch (error) { + if (error instanceof ValidationError) { + console.error(`Erreur (${error.typeErreur}): ${error.message}`); + } else { + console.error("Une erreur inattendue est survenue."); + } +} finally { + console.log("Fin du programme"); +} diff --git a/qualdev/TD_EX/exo4.js b/qualdev/TD_EX/exo4.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/qualdev/TD_EX/exo4.ts b/qualdev/TD_EX/exo4.ts new file mode 100644 index 0000000000000000000000000000000000000000..e90ea52ac5700fe003f98ce4829a917d52a009e6 --- /dev/null +++ b/qualdev/TD_EX/exo4.ts @@ -0,0 +1,20 @@ +function calcule( + tableau: Array<number>, + indice: number, + operateur: string, + operande: number +): number { + switch (operateur) { + case "+": + return tableau[indice] + operande; + case "-": + return tableau[indice] - operande; + case "*": + return tableau[indice] * operande; + case "/": + return tableau[indice] / operande; + } + return 0; +} + + diff --git a/qualdev/compte_rendu.txt b/qualdev/compte_rendu.txt index 54f849e28e1c0f1532763828adc4a4297ffdc282..f7cab9ce9050634f5f2335ad9818bc32e1503dea 100644 --- a/qualdev/compte_rendu.txt +++ b/qualdev/compte_rendu.txt @@ -126,3 +126,316 @@ Situation 3 : Travail sur le même fichier (conflit direct) git tag fin_tp git push origin fin_tp + + +Jeudi 13 mars + + +TD Exceptions + +EXERCICE 1 + +1. le code de base +function creeInitiales(nom: string, prenom: string): string { + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +le code fonctionne avec le nom et le prenom puis quand on enlève le nom en premier temps puis le prenom voila ce qui se prodruit: +TypeError: Cannot read properties of undefined (reading 'toUpperCase') + + at creeInitiales (/home/e84937u/TypeScript/qualdev/TD_Exceptions.js:2:19) + at Object.<anonymous> (/home/e84937u/TypeScript/qualdev/TD_Exceptions.js:4:13) + at Module._compile (node:internal/modules/cjs/loader:1469:14) + at Module._extensions..js (node:internal/modules/cjs/loader:1548:10) + at Module.load (node:internal/modules/cjs/loader:1288:32) + at Module._load (node:internal/modules/cjs/loader:1104:12) + at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:173:12) + at node:internal/main/run_main_module:28:49 + + + +2. C'est une exception de TypeError + + +3. Le code corriger : + +function creeInitiales(nom, prenom) { + if (!nom || !prenom) { + throw new Error("Les paramètres 'nom' et 'prenom' ne doivent pas être vides."); + } + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} +// Exemples de test +try { + console.log(creeInitiales("Bayiha", "Dieudonné")); + console.log(creeInitiales("", "Dieudonné")); +} +catch (error) { + console.error(error.message); +} + + + +4. class NomVideerror extends Error { + constructor(message: string) { + super(message); + this.name = "NomVideError"; + } +} +class PrenomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "PrenomVideError"; + } +} + +class NomEtPrenomVidesError extends Error { + constructor(message: string) { + super(message); + this.name = "NomEtPrenomVidesError"; + } +} + +5. Modification du programme. + +class NomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "NomVideError"; + } +} +class PrenomVideError extends Error { + constructor(message: string) { + super(message); + this.name = "PrenomVideError"; + } +} + +class NomEtPrenomVidesError extends Error { + constructor(message: string) { + super(message); + this.name = "NomEtPrenomVidesError"; + } +} + + +function creeInitiales(nom: string, prenom: string): string { + if(!nom && !prenom){ + throw new NomEtPrenomVidesError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides.");} + if (!nom){ + throw new NomVideError("Le paramètre 'nom' ne doit pas être vides.");} + if (!prenom){ + throw new PrenomVideError("Le paramètre'prenom' ne doit pas être vides.");} + + + + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +try { + console.log(creeInitiales("Bayiha", "Dieudonne")); + console.log(creeInitiales("Bayiha", "")); +} catch (error) { + console.error(error.message); +} + + +6 Avec des chaines vides + class NomVideError2 extends Error { + constructor(message: string) { + super(message); + this.name = "NomVideError"; + } +} +class PrenomVideError2 extends Error { + constructor(message: string) { + super(message); + this.name = "PrenomVideError"; + } +} + +class NomEtPrenomVidesError2 extends Error { + constructor(message: string) { + super(message); + this.name = "NomEtPrenomVidesError"; + } +} + +function creeInitiales2(nom: string, prenom: string): string { + if (!nom && !prenom) { + throw new NomEtPrenomVidesError2("Les paramètres 'nom' et 'prenom' ne doivent pas être vides."); + } + if (!nom) { + throw new NomVideError2("Le paramètre 'nom' ne doit pas être vide."); + } + if (!prenom) { + throw new PrenomVideError2("Le paramètre 'prenom' ne doit pas être vide."); + } + + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +try { + console.log(creeInitiales2("", "")); // +} catch (error) { + if (error instanceof NomEtPrenomVidesError2) { + console.error(`Erreur: ${error.message}`); + } else if (error instanceof NomVideError2) { + console.error(`Erreur: ${error.message}`); + } else if (error instanceof PrenomVideError2) { + console.error(`Erreur: ${error.message}`); + } else { + console.error("Une erreur inattendue est survenue."); + } +} finally { + console.log("Fin du programme"); +} + +console.log(creeInitiales2("", "Jean")); // Devrait lever NomVideError +console.log(creeInitiales2("Dupont", "")); // Devrait lever PrenomVideError +console.log(creeInitiales2("Dupont", "Jean")); // Devrait afficher "DJ" + + + + +7 Implémentation d'une solution + +class ValidationError extends Error { + typeErreur: string; + + constructor(message: string, typeErreur: string) { + super(message); + this.name = "ValidationError"; + this.typeErreur = typeErreur; + } +} + + +function creeInitiales2(nom: string, prenom: string): string { + if (!nom && !prenom) { + throw new ValidationError("Les paramètres 'nom' et 'prenom' ne doivent pas être vides.", "NomEtPrenomVides"); + } + if (!nom) { + throw new ValidationError("Le paramètre 'nom' ne doit pas être vide.", "NomVide"); + } + if (!prenom) { + throw new ValidationError("Le paramètre 'prenom' ne doit pas être vide.", "PrenomVide"); + } + + return nom[0].toUpperCase() + prenom[0].toUpperCase(); +} + + +try { + console.log(creeInitiales2("", "")); +} catch (error) { + if (error instanceof ValidationError) { + console.error(`Erreur (${error.typeErreur}): ${error.message}`); + } else { + console.error("Une erreur inattendue est survenue."); + } +} finally { + console.log("Fin du programme"); +} + + +EXERCICE 2 + +1 Classe etudiant + +class Etudiant { + constructor(nom: string, prenom: string, ine: number) { + this.nom = nom; + this.prenom = prenom; + this.ine = ine; + } + nom: string; + prenom: string; + ine: number; +} + + + + + + + + +TD_Debug + + + +Exercice 1 + +Dans cet exercice, il fallait placer un point d'arrêt pour voir comment évolue la variable j à chaque itération de la boucle. + +function deb1(): number { + let j = 200; + for (let i = 0; i < 10; i++) { + j -= 10; + } + return j; +} + +console.log(deb1()); + +Ce que j'ai appris : Avec le point d'arrêt, j'ai pu suivre les modifications de j à chaque passage dans la boucle for. Elle diminue de 10 à chaque fois, et au final, elle vaut 100. + +Exercice 2 + +function deb2(): number[] { + let j = 200; + let tab: number[] = []; + for (let i = 0; i < 10; i++) { + j -= 10; + tab[i] = j; + } + return tab; +} + +console.log(deb2()); +Grace à cette exercice en utilisant l'outil de débogage, j'ai pu voir comment le tableau tab se remplit progressivement avec les valeurs successives de j : [190, 180, 170, ..., 100]. + +Exercice 3 + +function deb2(x: number, y: number): void { + const tmp = y; + y = x; + x = tmp; + console.log(x, y); +} + +let x = 10; +let y = 20; +deb2(x, y); +console.log(x, y); + +Ce que j'ai appris : + +Ici, la fonction deb2 essaie d'échanger les valeurs de x et y. + +Cependant, les modifications sont locales à la fonction. En dehors de celle-ci, x et y conservent leurs valeurs d'origine (10 et 20). + +Le nom approprié pour cette fonction serait swapByValue pour indiquer que l'échange est uniquement local (par valeur). + +Exercice 4 + +function deb3(xy: number[]): void { + let tmp = xy[0]; + xy[0] = xy[1]; + xy[1] = tmp; + console.log(xy); +} + +let xy = [10, 20]; +deb3(xy); +console.log(xy); + +Ce que j'ai appris : + +Ici, l'échange fonctionne car les tableaux sont passés par référence. Cela signifie que les modifications faites sur le tableau dans la fonction sont directement reflétées en dehors de celle-ci. + +C'est pourquoi après l'appel de la fonction deb3, le tableau affiche [20, 10]. \ No newline at end of file diff --git a/quadldev b/qualdev/quadldev similarity index 100% rename from quadldev rename to qualdev/quadldev