fin tp
This commit is contained in:
parent
eb3de77bd9
commit
a17920df41
21
src/exercises/DoubleToUnique/index.test.ts
Normal file
21
src/exercises/DoubleToUnique/index.test.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import {removeDuplicates} from "./index";
|
||||
|
||||
describe("Test removeDuplicates", () => {
|
||||
it.each([
|
||||
{ arr: [3, 2, 4, 3, 5, 2], expected: [3, 2, 4, 5] },
|
||||
{ arr: [1, 1, 1, 1, 1], expected: [1] },
|
||||
{ arr: [1, 2, 3, 4, 5], expected: [1, 2, 3, 4, 5] }
|
||||
])(
|
||||
"should remove duplicates from array",
|
||||
({ arr, expected }) => {
|
||||
const result = removeDuplicates(arr);
|
||||
expect(result).toEqual(expected);
|
||||
console.log(`Test with array = ${arr}, expected = ${expected}, result = ${result}`);
|
||||
}
|
||||
);
|
||||
|
||||
it("should throw an exception when the array is empty", () => {
|
||||
const emptyArray: number[] = [];
|
||||
expect(() => removeDuplicates(emptyArray)).toThrowError("Le tableau est vide.");
|
||||
});
|
||||
});
|
13
src/exercises/DoubleToUnique/index.ts
Normal file
13
src/exercises/DoubleToUnique/index.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function removeDuplicates(arr: number[]): number[] {
|
||||
if (arr.length === 0) {
|
||||
throw new Error("Le tableau est vide.");
|
||||
}
|
||||
|
||||
const uniqueArray: number[] = [];
|
||||
arr.forEach((num) => {
|
||||
if (!uniqueArray.includes(num)) {
|
||||
uniqueArray.push(num);
|
||||
}
|
||||
});
|
||||
return uniqueArray;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
import {multiply} from "./index";
|
||||
import { multiply, multiplyArrays } from "./index";
|
||||
|
||||
describe("Test multiply", () => {
|
||||
it.each([
|
||||
@ -19,3 +19,16 @@ describe("Test multiply", () => {
|
||||
);
|
||||
});
|
||||
|
||||
describe('multiplyArrays', () => {
|
||||
test('multiplie les éléments de deux tableaux de même taille', () => {
|
||||
expect(multiplyArrays([3, 5, 2], [4, 3, 1])).toEqual([12, 15, 2]);
|
||||
});
|
||||
|
||||
test('lève une exception si les tableaux ne sont pas de même taille', () => {
|
||||
expect(() => multiplyArrays([1, 2, 3], [4, 5])).toThrowError('Les tableaux ne sont pas de même taille.');
|
||||
});
|
||||
|
||||
test('lève une exception si les tableaux sont vides', () => {
|
||||
expect(() => multiplyArrays([], [])).toThrowError('Les tableaux sont vides.');
|
||||
});
|
||||
});
|
@ -1,11 +1,22 @@
|
||||
async function start() {
|
||||
console.log("Application starts...");
|
||||
|
||||
console.log("Application ends...");
|
||||
}
|
||||
|
||||
start();
|
||||
|
||||
export function multiply (a : number, b : number) : number {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
export function multiplyArrays(arr1: number[], arr2: number[]): number[] {
|
||||
// Vérifier si les tableaux sont vides
|
||||
if (arr1.length === 0 || arr2.length === 0) {
|
||||
throw new Error("Les tableaux sont vides.");
|
||||
}
|
||||
|
||||
// Vérifier si les tableaux ont la même taille
|
||||
if (arr1.length !== arr2.length) {
|
||||
throw new Error("Les tableaux ne sont pas de même taille.");
|
||||
}
|
||||
|
||||
// Multiplier les éléments des tableaux
|
||||
const result: number[] = [];
|
||||
for (let i = 0; i < arr1.length; i++) {
|
||||
result.push(arr1[i] * arr2[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user