From 1eb0ffc86657ee8b9aee20eb61d628aed659deb7 Mon Sep 17 00:00:00 2001 From: khannurien Date: Fri, 16 Jan 2026 20:21:02 +0100 Subject: [PATCH] Exercices CM 1 --- exercice1.js | 7 ++++++ exercice1.ts | 19 +++++++++++++++ exercice2.js | 6 +++++ exercice2.ts | 15 ++++++++++++ exercice3.ts | 52 +++++++++++++++++++++++++++++++++++++++ exercice4.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ exercice5.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 230 insertions(+) create mode 100644 exercice1.js create mode 100644 exercice1.ts create mode 100644 exercice2.js create mode 100644 exercice2.ts create mode 100644 exercice3.ts create mode 100644 exercice4.js create mode 100644 exercice5.ts diff --git a/exercice1.js b/exercice1.js new file mode 100644 index 0000000..7fa719f --- /dev/null +++ b/exercice1.js @@ -0,0 +1,7 @@ +function addition(a, b) { + return a + b; +} + +console.log(addition(2, 3)); // attendu : 5 +console.log(addition("2", 3)); // attendu : ? +console.log(addition(true, 3)); // attendu : ? diff --git a/exercice1.ts b/exercice1.ts new file mode 100644 index 0000000..459be02 --- /dev/null +++ b/exercice1.ts @@ -0,0 +1,19 @@ +function addition(a, b) { + return a + b; +} + +console.log(addition(2, 3)); // attendu : 5 +console.log(addition("2", 3)); // attendu : ? +console.log(addition(true, 3)); // attendu : ? + +namespace Typed { + function addition(a: number, b: number): number { + return a + b; + } + + console.log(addition(2, 3)); + // L'argument de type 'string' n'est pas attribuable au paramètre de type 'number'.ts(2345) + console.log(addition("2", 3)); + // L'argument de type 'boolean' n'est pas attribuable au paramètre de type 'number'.ts(2345) + console.log(addition(true, 3)); +} \ No newline at end of file diff --git a/exercice2.js b/exercice2.js new file mode 100644 index 0000000..c6a9f3a --- /dev/null +++ b/exercice2.js @@ -0,0 +1,6 @@ +function afficherUtilisateur(user) { + console.log(`${user.nom} a ${user.age} ans`); +} + +afficherUtilisateur({ nom: "Alice", age: 25 }); +afficherUtilisateur({ nom: "Bob" }); // manque age diff --git a/exercice2.ts b/exercice2.ts new file mode 100644 index 0000000..68314ba --- /dev/null +++ b/exercice2.ts @@ -0,0 +1,15 @@ +interface Utilisateur { + nom: string; + age: number; +} + +function afficherUtilisateur(u: Utilisateur): void { + console.log(`${u.nom} a ${u.age} ans`); +} + +afficherUtilisateur({ nom: "Alice", age: 25 }); +// L'argument de type '{ nom: string; }' n'est pas attribuable +// au paramètre de type 'Utilisateur'. +// La propriété 'age' est absente du type '{ nom: string; }' +// mais obligatoire dans le type 'Utilisateur'.ts(2345) +afficherUtilisateur({ nom: "Bob" }); diff --git a/exercice3.ts b/exercice3.ts new file mode 100644 index 0000000..5804b85 --- /dev/null +++ b/exercice3.ts @@ -0,0 +1,52 @@ +interface Animal { + name: string; +} +interface Dog extends Animal { + bark: () => void; +} +interface Cat extends Animal { + meow: () => void; +} +const makeNoise = (animal: Animal) => { + if (animal instanceof Dog) { + // 'Dog' fait uniquement référence à un type mais s'utilise en tant que valeur ici.ts(2693) + animal.bark(); + } +}; + +namespace TaggedUnion { + interface Dog { + kind: "dog"; // Tagged union + bark: () => void; + } + interface Cat { + kind: "cat"; // Tagged union + meow: () => void; + } + type Animal = Dog | Cat; + const makeNoise = (animal: Animal) => { + if (animal.kind === "dog") animal.bark(); + else animal.meow(); + }; + const dog: Dog = { + kind: "dog", + bark: () => console.log("bark"), + }; + makeNoise(dog); +} + +namespace Classes { + class Dog { + constructor(public name: string, public bark: () => void) {} + } + class Cat { + constructor(public name: string, public meow: () => void) {} + } + type Animal = Dog | Cat; + const makeNoise = (animal: Animal) => { + if (animal instanceof Dog) animal.bark(); + else animal.meow(); + }; + const dog = new Dog("Fido", () => console.log("bark")); + makeNoise(dog); +} \ No newline at end of file diff --git a/exercice4.js b/exercice4.js new file mode 100644 index 0000000..ee35c07 --- /dev/null +++ b/exercice4.js @@ -0,0 +1,69 @@ +function readFile(path, charset) { + throw new Error("Function not implemented."); +} + +function readTextFileIfExists(path) { + return new Promise((resolve, reject) => { + if (!path) { + reject(new Error("No file path provided")); + return; + } + + readFile(path, "utf8") + .then(data => { + if (data.length === 0) { + reject(new Error("File is empty")); // explicit failure + } else { + resolve(data); // successful read + } + }) + .catch(err => reject(err)); // I/O error + }); +} + +readTextFileIfExists("data.txt") + .then(text => console.log("File contents:", text)) + .catch(err => console.error("I/O error:", err.message)); + +try { + const text = await readTextFileIfExists("data.txt"); + console.log("File contents:", text); +} catch (err) { + console.error("I/O error:", err.message); +} + +const files = ["file1.txt", "file2.txt", "file3.txt"]; + +for (const file of files) { + try { + const text = await readTextFileIfExists(file); + console.log("File contents:", text); + } catch (err) { + console.error("I/O error:", err.message); + continue; + } +} + +const otherFiles = ["does_not_exist.txt", "file2.txt", "file3.txt"]; + +const failContents = await Promise.all( + otherFiles.map(readTextFileIfExists) +); +try { + failContents.forEach((text, i) => { + console.log("File contents:", text); + }); +} catch (err) { + console.error("I/O error:", err.message); +} + +const successContents = await Promise.allSettled( + files.map(readTextFileIfExists) +); +successContents.forEach((result, i) => { + if (result.status === "fulfilled") { + console.log("File contents:", result.value); + } else if (result.status === "rejected") { + console.error("I/O error:", result.reason); + } +}); \ No newline at end of file diff --git a/exercice5.ts b/exercice5.ts new file mode 100644 index 0000000..6575e7c --- /dev/null +++ b/exercice5.ts @@ -0,0 +1,62 @@ +const listener = Deno.listen({ port: 8080 }); + +const decoder = new TextDecoder(); +const encoder = new TextEncoder(); + +for await (const conn of listener) { + handleConn(conn); +} + +async function handleConn(conn: Deno.Conn) { + const buf = new Uint8Array(1024); + const bytes = await conn.read(buf); + if (bytes === null) { + conn.close(); + return; + } + + const rawRequest = decoder.decode(buf.subarray(0, bytes)); + + console.log(rawRequest); + + const [ + requestMethod, + requestHost, + requestUserAgent, + requestAccept, + requestContentType, + requestContentLength, + _, + requestBody, + ] = rawRequest.split("\r\n"); + + const [method, path, protocol] = requestMethod.split(" "); + const [, host] = requestHost.split(" "); + const [, userAgent] = requestUserAgent.split(" "); + const [, accept] = requestAccept.split(" "); + const [, contentType] = requestContentType.split(" "); + const [, contentLength] = requestContentLength.split(" "); + + const responseBody = `{` + + `"method": "${method}",` + + `"path": "${path}",` + + `"protocol": "${protocol}",` + + `"host": "${host}",` + + `"user-agent": "${userAgent}",` + + `"accept": "${accept}",` + + `"content-type": "${contentType}",` + + `"content-length": "${contentLength}",` + + `"body": "${requestBody}"` + + `}\n`; + + const response = + "HTTP/1.1 200 OK\r\n" + + "Content-Type: text/json\r\n" + + `Content-Length: ${responseBody.length}\r\n` + + "Connection: close\r\n" + + "\r\n" + + responseBody; + + await conn.write(encoder.encode(response)); + conn.close(); +}