Exercices CM 1
This commit is contained in:
7
exercice1.js
Normal file
7
exercice1.js
Normal file
@@ -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 : ?
|
||||||
19
exercice1.ts
Normal file
19
exercice1.ts
Normal file
@@ -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));
|
||||||
|
}
|
||||||
6
exercice2.js
Normal file
6
exercice2.js
Normal file
@@ -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
|
||||||
15
exercice2.ts
Normal file
15
exercice2.ts
Normal file
@@ -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" });
|
||||||
52
exercice3.ts
Normal file
52
exercice3.ts
Normal file
@@ -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);
|
||||||
|
}
|
||||||
69
exercice4.js
Normal file
69
exercice4.js
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
62
exercice5.ts
Normal file
62
exercice5.ts
Normal file
@@ -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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user