This commit is contained in:
OMGiTzPomPom 2024-03-29 16:57:33 +01:00
parent eb3de77bd9
commit a17920df41
4 changed files with 67 additions and 9 deletions

View 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.");
});
});

View 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;
}

View File

@ -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.');
});
});

View File

@ -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;
}