diff --git a/exo7.ts b/exo7.ts index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..23da63be641fa8e2a85d32a71c5f105a65914db7 100644 --- a/exo7.ts +++ b/exo7.ts @@ -0,0 +1,28 @@ +function multiply(x: number, y: number): number { + + if (y < 0) { + x = -x; + y = -y; + } + + let result = 0; + + for (let i = 0; i < y; i++) { + result += x; + } + return result; +} + + +function testMultiply() { + const testCases = [ + [0, 0], [5, 3], [3, 5], [-5, -2], [-2, -5], + [-74, 2], [-1, 75], [10, -25], [10, -3] + ]; + + for (const [x, y] of testCases) { + console.log(`multiply(${x}, ${y}) = ${multiply(x, y)}`); + } +} + +testMultiply(); \ No newline at end of file