diff --git a/src/exercises/DoubleToUnique/index.test.ts b/src/exercises/DoubleToUnique/index.test.ts new file mode 100644 index 0000000..0a20398 --- /dev/null +++ b/src/exercises/DoubleToUnique/index.test.ts @@ -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."); + }); +}); \ No newline at end of file diff --git a/src/exercises/DoubleToUnique/index.ts b/src/exercises/DoubleToUnique/index.ts new file mode 100644 index 0000000..74ec376 --- /dev/null +++ b/src/exercises/DoubleToUnique/index.ts @@ -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; +} diff --git a/src/exercises/multiply/index.test.ts b/src/exercises/multiply/index.test.ts index 387d404..bbae25d 100644 --- a/src/exercises/multiply/index.test.ts +++ b/src/exercises/multiply/index.test.ts @@ -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.'); + }); +}); \ No newline at end of file diff --git a/src/exercises/multiply/index.ts b/src/exercises/multiply/index.ts index 2312890..4029eb3 100644 --- a/src/exercises/multiply/index.ts +++ b/src/exercises/multiply/index.ts @@ -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; } \ No newline at end of file