Home > katas > Multiply TypeScript (8kyu)

Multiply TypeScript (8kyu)


Let's start with Multiply kata. I have to solve this one when I signed up for the Codewars account. It is some kind of validation, I guess.

Chek kata on Codewars

Description:

The code does not execute properly. Try to figure out why.

We start with this code:

export function multiply(a: number, b: number): number {
  return a + b;
}

We can solve it just by replacing - with +

export function multiply(a: number, b: number): number {
  return a * b;
}

Or we can do oneliner

export const multiply = (a: number, b: number): number => a * b;

Let's check others solutions.

Some people use reducer to multiply two numbers. As for me, it's a little bit overkill. But some people like it, so why not.

export function multiply(a: number, b: number): number {
  return [a,b].reduce((accum, curr) => accum * curr, 1)
}
© 2021, Andrew Losseff